am 20d93db1: am cdb621bf: retry patch using cache if in-place write fails

Merge commit '20d93db18de9001dcb5af46e4d1d2bac10aa2eca'

* commit '20d93db18de9001dcb5af46e4d1d2bac10aa2eca':
  retry patch using cache if in-place write fails
This commit is contained in:
Doug Zongker 2010-02-02 09:38:39 -08:00 committed by Android Git Automerger
commit 30f1d2cf29
5 changed files with 282 additions and 239 deletions

View file

@ -21,6 +21,8 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/statfs.h> #include <sys/statfs.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include "mincrypt/sha.h" #include "mincrypt/sha.h"
@ -30,6 +32,7 @@
int SaveFileContents(const char* filename, FileContents file); int SaveFileContents(const char* filename, FileContents file);
int LoadMTDContents(const char* filename, FileContents* file); int LoadMTDContents(const char* filename, FileContents* file);
int ParseSha1(const char* str, uint8_t* digest); int ParseSha1(const char* str, uint8_t* digest);
size_t FileSink(unsigned char* data, size_t len, void* token);
static int mtd_partitions_scanned = 0; static int mtd_partitions_scanned = 0;
@ -45,7 +48,7 @@ int LoadFileContents(const char* filename, FileContents* file) {
} }
if (stat(filename, &file->st) != 0) { if (stat(filename, &file->st) != 0) {
fprintf(stderr, "failed to stat \"%s\": %s\n", filename, strerror(errno)); printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
return -1; return -1;
} }
@ -54,7 +57,7 @@ int LoadFileContents(const char* filename, FileContents* file) {
FILE* f = fopen(filename, "rb"); FILE* f = fopen(filename, "rb");
if (f == NULL) { if (f == NULL) {
fprintf(stderr, "failed to open \"%s\": %s\n", filename, strerror(errno)); printf("failed to open \"%s\": %s\n", filename, strerror(errno));
free(file->data); free(file->data);
file->data = NULL; file->data = NULL;
return -1; return -1;
@ -62,7 +65,7 @@ int LoadFileContents(const char* filename, FileContents* file) {
size_t bytes_read = fread(file->data, 1, file->size, f); size_t bytes_read = fread(file->data, 1, file->size, f);
if (bytes_read != file->size) { if (bytes_read != file->size) {
fprintf(stderr, "short read of \"%s\" (%d bytes of %d)\n", printf("short read of \"%s\" (%d bytes of %d)\n",
filename, bytes_read, file->size); filename, bytes_read, file->size);
free(file->data); free(file->data);
file->data = NULL; file->data = NULL;
@ -108,7 +111,7 @@ int LoadMTDContents(const char* filename, FileContents* file) {
char* copy = strdup(filename); char* copy = strdup(filename);
const char* magic = strtok(copy, ":"); const char* magic = strtok(copy, ":");
if (strcmp(magic, "MTD") != 0) { if (strcmp(magic, "MTD") != 0) {
fprintf(stderr, "LoadMTDContents called with bad filename (%s)\n", printf("LoadMTDContents called with bad filename (%s)\n",
filename); filename);
return -1; return -1;
} }
@ -122,7 +125,7 @@ int LoadMTDContents(const char* filename, FileContents* file) {
} }
} }
if (colons < 3 || colons%2 == 0) { if (colons < 3 || colons%2 == 0) {
fprintf(stderr, "LoadMTDContents called with bad filename (%s)\n", printf("LoadMTDContents called with bad filename (%s)\n",
filename); filename);
} }
@ -135,7 +138,7 @@ int LoadMTDContents(const char* filename, FileContents* file) {
const char* size_str = strtok(NULL, ":"); const char* size_str = strtok(NULL, ":");
size[i] = strtol(size_str, NULL, 10); size[i] = strtol(size_str, NULL, 10);
if (size[i] == 0) { if (size[i] == 0) {
fprintf(stderr, "LoadMTDContents called with bad size (%s)\n", filename); printf("LoadMTDContents called with bad size (%s)\n", filename);
return -1; return -1;
} }
sha1sum[i] = strtok(NULL, ":"); sha1sum[i] = strtok(NULL, ":");
@ -154,14 +157,14 @@ int LoadMTDContents(const char* filename, FileContents* file) {
const MtdPartition* mtd = mtd_find_partition_by_name(partition); const MtdPartition* mtd = mtd_find_partition_by_name(partition);
if (mtd == NULL) { if (mtd == NULL) {
fprintf(stderr, "mtd partition \"%s\" not found (loading %s)\n", printf("mtd partition \"%s\" not found (loading %s)\n",
partition, filename); partition, filename);
return -1; return -1;
} }
MtdReadContext* ctx = mtd_read_partition(mtd); MtdReadContext* ctx = mtd_read_partition(mtd);
if (ctx == NULL) { if (ctx == NULL) {
fprintf(stderr, "failed to initialize read of mtd partition \"%s\"\n", printf("failed to initialize read of mtd partition \"%s\"\n",
partition); partition);
return -1; return -1;
} }
@ -184,7 +187,7 @@ int LoadMTDContents(const char* filename, FileContents* file) {
if (next > 0) { if (next > 0) {
read = mtd_read_data(ctx, p, next); read = mtd_read_data(ctx, p, next);
if (next != read) { if (next != read) {
fprintf(stderr, "short read (%d bytes of %d) for partition \"%s\"\n", printf("short read (%d bytes of %d) for partition \"%s\"\n",
read, next, partition); read, next, partition);
free(file->data); free(file->data);
file->data = NULL; file->data = NULL;
@ -201,7 +204,7 @@ int LoadMTDContents(const char* filename, FileContents* file) {
const uint8_t* sha_so_far = SHA_final(&temp_ctx); const uint8_t* sha_so_far = SHA_final(&temp_ctx);
if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) { if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) {
fprintf(stderr, "failed to parse sha1 %s in %s\n", printf("failed to parse sha1 %s in %s\n",
sha1sum[index[i]], filename); sha1sum[index[i]], filename);
free(file->data); free(file->data);
file->data = NULL; file->data = NULL;
@ -224,7 +227,7 @@ int LoadMTDContents(const char* filename, FileContents* file) {
if (i == pairs) { if (i == pairs) {
// Ran off the end of the list of (size,sha1) pairs without // Ran off the end of the list of (size,sha1) pairs without
// finding a match. // finding a match.
fprintf(stderr, "contents of MTD partition \"%s\" didn't match %s\n", printf("contents of MTD partition \"%s\" didn't match %s\n",
partition, filename); partition, filename);
free(file->data); free(file->data);
file->data = NULL; file->data = NULL;
@ -253,29 +256,29 @@ int LoadMTDContents(const char* filename, FileContents* file) {
// Save the contents of the given FileContents object under the given // Save the contents of the given FileContents object under the given
// filename. Return 0 on success. // filename. Return 0 on success.
int SaveFileContents(const char* filename, FileContents file) { int SaveFileContents(const char* filename, FileContents file) {
FILE* f = fopen(filename, "wb"); int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
if (f == NULL) { if (fd < 0) {
fprintf(stderr, "failed to open \"%s\" for write: %s\n", printf("failed to open \"%s\" for write: %s\n",
filename, strerror(errno)); filename, strerror(errno));
return -1; return -1;
} }
size_t bytes_written = fwrite(file.data, 1, file.size, f); size_t bytes_written = FileSink(file.data, file.size, &fd);
if (bytes_written != file.size) { if (bytes_written != file.size) {
fprintf(stderr, "short write of \"%s\" (%d bytes of %d)\n", printf("short write of \"%s\" (%d bytes of %d) (%s)\n",
filename, bytes_written, file.size); filename, bytes_written, file.size, strerror(errno));
close(fd);
return -1; return -1;
} }
fflush(f); fsync(fd);
fsync(fileno(f)); close(fd);
fclose(f);
if (chmod(filename, file.st.st_mode) != 0) { if (chmod(filename, file.st.st_mode) != 0) {
fprintf(stderr, "chmod of \"%s\" failed: %s\n", filename, strerror(errno)); printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
return -1; return -1;
} }
if (chown(filename, file.st.st_uid, file.st.st_gid) != 0) { if (chown(filename, file.st.st_uid, file.st.st_gid) != 0) {
fprintf(stderr, "chown of \"%s\" failed: %s\n", filename, strerror(errno)); printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
return -1; return -1;
} }
@ -288,7 +291,7 @@ int WriteToMTDPartition(unsigned char* data, size_t len,
const char* target_mtd) { const char* target_mtd) {
char* partition = strchr(target_mtd, ':'); char* partition = strchr(target_mtd, ':');
if (partition == NULL) { if (partition == NULL) {
fprintf(stderr, "bad MTD target name \"%s\"\n", target_mtd); printf("bad MTD target name \"%s\"\n", target_mtd);
return -1; return -1;
} }
++partition; ++partition;
@ -306,33 +309,33 @@ int WriteToMTDPartition(unsigned char* data, size_t len,
const MtdPartition* mtd = mtd_find_partition_by_name(partition); const MtdPartition* mtd = mtd_find_partition_by_name(partition);
if (mtd == NULL) { if (mtd == NULL) {
fprintf(stderr, "mtd partition \"%s\" not found for writing\n", partition); printf("mtd partition \"%s\" not found for writing\n", partition);
return -1; return -1;
} }
MtdWriteContext* ctx = mtd_write_partition(mtd); MtdWriteContext* ctx = mtd_write_partition(mtd);
if (ctx == NULL) { if (ctx == NULL) {
fprintf(stderr, "failed to init mtd partition \"%s\" for writing\n", printf("failed to init mtd partition \"%s\" for writing\n",
partition); partition);
return -1; return -1;
} }
size_t written = mtd_write_data(ctx, (char*)data, len); size_t written = mtd_write_data(ctx, (char*)data, len);
if (written != len) { if (written != len) {
fprintf(stderr, "only wrote %d of %d bytes to MTD %s\n", printf("only wrote %d of %d bytes to MTD %s\n",
written, len, partition); written, len, partition);
mtd_write_close(ctx); mtd_write_close(ctx);
return -1; return -1;
} }
if (mtd_erase_blocks(ctx, -1) < 0) { if (mtd_erase_blocks(ctx, -1) < 0) {
fprintf(stderr, "error finishing mtd write of %s\n", partition); printf("error finishing mtd write of %s\n", partition);
mtd_write_close(ctx); mtd_write_close(ctx);
return -1; return -1;
} }
if (mtd_write_close(ctx)) { if (mtd_write_close(ctx)) {
fprintf(stderr, "error closing mtd write of %s\n", partition); printf("error closing mtd write of %s\n", partition);
return -1; return -1;
} }
@ -381,7 +384,7 @@ int ParseShaArgs(int argc, char** argv, Patch** patches, int* num_patches) {
int i; int i;
for (i = 0; i < *num_patches; ++i) { for (i = 0; i < *num_patches; ++i) {
if (ParseSha1(argv[i], (*patches)[i].sha1) != 0) { if (ParseSha1(argv[i], (*patches)[i].sha1) != 0) {
fprintf(stderr, "failed to parse sha1 \"%s\"\n", argv[i]); printf("failed to parse sha1 \"%s\"\n", argv[i]);
return -1; return -1;
} }
if (argv[i][SHA_DIGEST_SIZE*2] == '\0') { if (argv[i][SHA_DIGEST_SIZE*2] == '\0') {
@ -389,7 +392,7 @@ int ParseShaArgs(int argc, char** argv, Patch** patches, int* num_patches) {
} else if (argv[i][SHA_DIGEST_SIZE*2] == ':') { } else if (argv[i][SHA_DIGEST_SIZE*2] == ':') {
(*patches)[i].patch_filename = argv[i] + (SHA_DIGEST_SIZE*2+1); (*patches)[i].patch_filename = argv[i] + (SHA_DIGEST_SIZE*2+1);
} else { } else {
fprintf(stderr, "failed to parse filename \"%s\"\n", argv[i]); printf("failed to parse filename \"%s\"\n", argv[i]);
return -1; return -1;
} }
} }
@ -414,7 +417,7 @@ const Patch* FindMatchingPatch(uint8_t* sha1, Patch* patches, int num_patches) {
// nonzero otherwise. // nonzero otherwise.
int CheckMode(int argc, char** argv) { int CheckMode(int argc, char** argv) {
if (argc < 3) { if (argc < 3) {
fprintf(stderr, "no filename given\n"); printf("no filename given\n");
return 2; return 2;
} }
@ -432,7 +435,7 @@ int CheckMode(int argc, char** argv) {
if (LoadFileContents(argv[2], &file) != 0 || if (LoadFileContents(argv[2], &file) != 0 ||
(num_patches > 0 && (num_patches > 0 &&
FindMatchingPatch(file.sha1, patches, num_patches) == NULL)) { FindMatchingPatch(file.sha1, patches, num_patches) == NULL)) {
fprintf(stderr, "file \"%s\" doesn't have any of expected " printf("file \"%s\" doesn't have any of expected "
"sha1 sums; checking cache\n", argv[2]); "sha1 sums; checking cache\n", argv[2]);
free(file.data); free(file.data);
@ -444,12 +447,12 @@ int CheckMode(int argc, char** argv) {
// passes. // passes.
if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) { if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
fprintf(stderr, "failed to load cache file\n"); printf("failed to load cache file\n");
return 1; return 1;
} }
if (FindMatchingPatch(file.sha1, patches, num_patches) == NULL) { if (FindMatchingPatch(file.sha1, patches, num_patches) == NULL) {
fprintf(stderr, "cache bits don't match any sha1 for \"%s\"\n", printf("cache bits don't match any sha1 for \"%s\"\n",
argv[2]); argv[2]);
return 1; return 1;
} }
@ -465,7 +468,19 @@ int ShowLicenses() {
} }
size_t FileSink(unsigned char* data, size_t len, void* token) { size_t FileSink(unsigned char* data, size_t len, void* token) {
return fwrite(data, 1, len, (FILE*)token); int fd = *(int *)token;
ssize_t done = 0;
ssize_t wrote;
while (done < (ssize_t) len) {
wrote = write(fd, data+done, len-done);
if (wrote <= 0) {
printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno));
return done;
}
done += wrote;
}
printf("wrote %d bytes to output\n", (int)done);
return done;
} }
typedef struct { typedef struct {
@ -489,7 +504,7 @@ size_t MemorySink(unsigned char* data, size_t len, void* token) {
size_t FreeSpaceForFile(const char* filename) { size_t FreeSpaceForFile(const char* filename) {
struct statfs sf; struct statfs sf;
if (statfs(filename, &sf) != 0) { if (statfs(filename, &sf) != 0) {
fprintf(stderr, "failed to statfs %s: %s\n", filename, strerror(errno)); printf("failed to statfs %s: %s\n", filename, strerror(errno));
return -1; return -1;
} }
return sf.f_bsize * sf.f_bfree; return sf.f_bsize * sf.f_bfree;
@ -583,8 +598,10 @@ int applypatch(int argc, char** argv) {
target_filename = source_filename; target_filename = source_filename;
} }
if (ParseSha1(argv[3], target_sha1) != 0) { printf("\napplying patch to %s\n", source_filename);
fprintf(stderr, "failed to parse tgt-sha1 \"%s\"\n", argv[3]);
if (ParseSha1(argv[3], target_sha1) != 0) {
printf("failed to parse tgt-sha1 \"%s\"\n", argv[3]);
return 1; return 1;
} }
@ -605,7 +622,7 @@ int applypatch(int argc, char** argv) {
if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) {
// The early-exit case: the patch was already applied, this file // The early-exit case: the patch was already applied, this file
// has the desired hash, nothing for us to do. // has the desired hash, nothing for us to do.
fprintf(stderr, "\"%s\" is already target; no patch needed\n", printf("\"%s\" is already target; no patch needed\n",
target_filename); target_filename);
return 0; return 0;
} }
@ -630,11 +647,11 @@ int applypatch(int argc, char** argv) {
if (source_patch_filename == NULL) { if (source_patch_filename == NULL) {
free(source_file.data); free(source_file.data);
fprintf(stderr, "source file is bad; trying copy\n"); printf("source file is bad; trying copy\n");
if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) { if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
// fail. // fail.
fprintf(stderr, "failed to read copy file\n"); printf("failed to read copy file\n");
return 1; return 1;
} }
@ -646,179 +663,205 @@ int applypatch(int argc, char** argv) {
if (copy_patch_filename == NULL) { if (copy_patch_filename == NULL) {
// fail. // fail.
fprintf(stderr, "copy file doesn't match source SHA-1s either\n"); printf("copy file doesn't match source SHA-1s either\n");
return 1; return 1;
} }
} }
// Is there enough room in the target filesystem to hold the patched int retry = 1;
// file? SHA_CTX ctx;
int output;
MemorySinkInfo msi;
FileContents* source_to_use;
char* outname;
if (strncmp(target_filename, "MTD:", 4) == 0) { // assume that target_filename (eg "/system/app/Foo.apk") is located
// If the target is an MTD partition, we're actually going to // on the same filesystem as its top-level directory ("/system").
// write the output to /tmp and then copy it to the partition. // We need something that exists for calling statfs().
// statfs() always returns 0 blocks free for /tmp, so instead char target_fs[strlen(target_filename)+1];
// we'll just assume that /tmp has enough space to hold the file. char* slash = strchr(target_filename+1, '/');
if (slash != NULL) {
// We still write the original source to cache, in case the MTD int count = slash - target_filename;
// write is interrupted. strncpy(target_fs, target_filename, count);
if (MakeFreeSpaceOnCache(source_file.size) < 0) { target_fs[count] = '\0';
fprintf(stderr, "not enough free space on /cache\n");
return 1;
}
if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
fprintf(stderr, "failed to back up source file\n");
return 1;
}
made_copy = 1;
} else { } else {
// assume that target_filename (eg "/system/app/Foo.apk") is located strcpy(target_fs, target_filename);
// on the same filesystem as its top-level directory ("/system"). }
// We need something that exists for calling statfs().
char* target_fs = strdup(target_filename);
char* slash = strchr(target_fs+1, '/');
if (slash != NULL) {
*slash = '\0';
}
size_t free_space = FreeSpaceForFile(target_fs); do {
int enough_space = // Is there enough room in the target filesystem to hold the patched
free_space > (target_size * 3 / 2); // 50% margin of error // file?
printf("target %ld bytes; free space %ld bytes; enough %d\n",
(long)target_size, (long)free_space, enough_space);
if (!enough_space && source_patch_filename != NULL) { if (strncmp(target_filename, "MTD:", 4) == 0) {
// Using the original source, but not enough free space. First // If the target is an MTD partition, we're actually going to
// copy the source file to cache, then delete it from the original // write the output to /tmp and then copy it to the partition.
// location. // statfs() always returns 0 blocks free for /tmp, so instead
// we'll just assume that /tmp has enough space to hold the file.
if (strncmp(source_filename, "MTD:", 4) == 0) {
// It's impossible to free space on the target filesystem by
// deleting the source if the source is an MTD partition. If
// we're ever in a state where we need to do this, fail.
fprintf(stderr, "not enough free space for target but source is MTD\n");
return 1;
}
// We still write the original source to cache, in case the MTD
// write is interrupted.
if (MakeFreeSpaceOnCache(source_file.size) < 0) { if (MakeFreeSpaceOnCache(source_file.size) < 0) {
fprintf(stderr, "not enough free space on /cache\n"); printf("not enough free space on /cache\n");
return 1; return 1;
} }
if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) { if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
fprintf(stderr, "failed to back up source file\n"); printf("failed to back up source file\n");
return 1; return 1;
} }
made_copy = 1; made_copy = 1;
unlink(source_filename); retry = 0;
} else {
int enough_space = 0;
if (retry > 0) {
size_t free_space = FreeSpaceForFile(target_fs);
int enough_space =
(free_space > (target_size * 3 / 2)); // 50% margin of error
printf("target %ld bytes; free space %ld bytes; retry %d; enough %d\n",
(long)target_size, (long)free_space, retry, enough_space);
}
size_t free_space = FreeSpaceForFile(target_fs); if (!enough_space) {
printf("(now %ld bytes free for target)\n", (long)free_space); retry = 0;
}
if (!enough_space && source_patch_filename != NULL) {
// Using the original source, but not enough free space. First
// copy the source file to cache, then delete it from the original
// location.
if (strncmp(source_filename, "MTD:", 4) == 0) {
// It's impossible to free space on the target filesystem by
// deleting the source if the source is an MTD partition. If
// we're ever in a state where we need to do this, fail.
printf("not enough free space for target but source is MTD\n");
return 1;
}
if (MakeFreeSpaceOnCache(source_file.size) < 0) {
printf("not enough free space on /cache\n");
return 1;
}
if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
printf("failed to back up source file\n");
return 1;
}
made_copy = 1;
unlink(source_filename);
size_t free_space = FreeSpaceForFile(target_fs);
printf("(now %ld bytes free for target)\n", (long)free_space);
}
} }
}
FileContents* source_to_use; const char* patch_filename;
const char* patch_filename; if (source_patch_filename != NULL) {
if (source_patch_filename != NULL) { source_to_use = &source_file;
source_to_use = &source_file; patch_filename = source_patch_filename;
patch_filename = source_patch_filename; } else {
} else { source_to_use = &copy_file;
source_to_use = &copy_file; patch_filename = copy_patch_filename;
patch_filename = copy_patch_filename;
}
char* outname = NULL;
FILE* output = NULL;
MemorySinkInfo msi;
SinkFn sink = NULL;
void* token = NULL;
if (strncmp(target_filename, "MTD:", 4) == 0) {
// We store the decoded output in memory.
msi.buffer = malloc(target_size);
if (msi.buffer == NULL) {
fprintf(stderr, "failed to alloc %ld bytes for output\n",
(long)target_size);
return 1;
} }
msi.pos = 0;
msi.size = target_size;
sink = MemorySink;
token = &msi;
} else {
// We write the decoded output to "<tgt-file>.patch".
outname = (char*)malloc(strlen(target_filename) + 10);
strcpy(outname, target_filename);
strcat(outname, ".patch");
output = fopen(outname, "wb"); SinkFn sink = NULL;
if (output == NULL) { void* token = NULL;
fprintf(stderr, "failed to open output file %s: %s\n", output = -1;
outname, strerror(errno)); outname = NULL;
return 1; if (strncmp(target_filename, "MTD:", 4) == 0) {
// We store the decoded output in memory.
msi.buffer = malloc(target_size);
if (msi.buffer == NULL) {
printf("failed to alloc %ld bytes for output\n",
(long)target_size);
return 1;
}
msi.pos = 0;
msi.size = target_size;
sink = MemorySink;
token = &msi;
} else {
// We write the decoded output to "<tgt-file>.patch".
outname = (char*)malloc(strlen(target_filename) + 10);
strcpy(outname, target_filename);
strcat(outname, ".patch");
output = open(outname, O_WRONLY | O_CREAT | O_TRUNC);
if (output < 0) {
printf("failed to open output file %s: %s\n",
outname, strerror(errno));
return 1;
}
sink = FileSink;
token = &output;
} }
sink = FileSink;
token = output;
}
#define MAX_HEADER_LENGTH 8 #define MAX_HEADER_LENGTH 8
unsigned char header[MAX_HEADER_LENGTH]; unsigned char header[MAX_HEADER_LENGTH];
FILE* patchf = fopen(patch_filename, "rb"); FILE* patchf = fopen(patch_filename, "rb");
if (patchf == NULL) { if (patchf == NULL) {
fprintf(stderr, "failed to open patch file %s: %s\n", printf("failed to open patch file %s: %s\n",
patch_filename, strerror(errno)); patch_filename, strerror(errno));
return 1; return 1;
}
int header_bytes_read = fread(header, 1, MAX_HEADER_LENGTH, patchf);
fclose(patchf);
SHA_CTX ctx;
SHA_init(&ctx);
if (header_bytes_read >= 4 &&
header[0] == 0xd6 && header[1] == 0xc3 &&
header[2] == 0xc4 && header[3] == 0) {
// xdelta3 patches begin "VCD" (with the high bits set) followed
// by a zero byte (the version number).
fprintf(stderr, "error: xdelta3 patches no longer supported\n");
return 1;
} else if (header_bytes_read >= 8 &&
memcmp(header, "BSDIFF40", 8) == 0) {
int result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
patch_filename, 0, sink, token, &ctx);
if (result != 0) {
fprintf(stderr, "ApplyBSDiffPatch failed\n");
return result;
} }
} else if (header_bytes_read >= 8 && int header_bytes_read = fread(header, 1, MAX_HEADER_LENGTH, patchf);
memcmp(header, "IMGDIFF", 7) == 0 && fclose(patchf);
(header[7] == '1' || header[7] == '2')) {
int result = ApplyImagePatch(source_to_use->data, source_to_use->size,
patch_filename, sink, token, &ctx);
if (result != 0) {
fprintf(stderr, "ApplyImagePatch failed\n");
return result;
}
} else {
fprintf(stderr, "Unknown patch file format\n");
return 1;
}
if (output != NULL) { SHA_init(&ctx);
fflush(output);
fsync(fileno(output)); int result;
fclose(output);
} if (header_bytes_read >= 4 &&
header[0] == 0xd6 && header[1] == 0xc3 &&
header[2] == 0xc4 && header[3] == 0) {
// xdelta3 patches begin "VCD" (with the high bits set) followed
// by a zero byte (the version number).
printf("error: xdelta3 patches no longer supported\n");
return 1;
} else if (header_bytes_read >= 8 &&
memcmp(header, "BSDIFF40", 8) == 0) {
result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
patch_filename, 0, sink, token, &ctx);
} else if (header_bytes_read >= 8 &&
memcmp(header, "IMGDIFF", 7) == 0 &&
(header[7] == '1' || header[7] == '2')) {
result = ApplyImagePatch(source_to_use->data, source_to_use->size,
patch_filename, sink, token, &ctx);
} else {
printf("Unknown patch file format\n");
return 1;
}
if (output >= 0) {
fsync(output);
close(output);
}
if (result != 0) {
if (retry == 0) {
printf("applying patch failed\n");
return result;
} else {
printf("applying patch failed; retrying\n");
}
if (outname != NULL) {
unlink(outname);
}
} else {
// succeeded; no need to retry
break;
}
} while (retry-- > 0);
const uint8_t* current_target_sha1 = SHA_final(&ctx); const uint8_t* current_target_sha1 = SHA_final(&ctx);
if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) {
fprintf(stderr, "patch did not produce expected sha1\n"); printf("patch did not produce expected sha1\n");
return 1; return 1;
} }
if (output == NULL) { if (output < 0) {
// Copy the temp file to the MTD partition. // Copy the temp file to the MTD partition.
if (WriteToMTDPartition(msi.buffer, msi.pos, target_filename) != 0) { if (WriteToMTDPartition(msi.buffer, msi.pos, target_filename) != 0) {
fprintf(stderr, "write of patched data to %s failed\n", target_filename); printf("write of patched data to %s failed\n", target_filename);
return 1; return 1;
} }
free(msi.buffer); free(msi.buffer);
@ -826,18 +869,18 @@ int applypatch(int argc, char** argv) {
// Give the .patch file the same owner, group, and mode of the // Give the .patch file the same owner, group, and mode of the
// original source file. // original source file.
if (chmod(outname, source_to_use->st.st_mode) != 0) { if (chmod(outname, source_to_use->st.st_mode) != 0) {
fprintf(stderr, "chmod of \"%s\" failed: %s\n", outname, strerror(errno)); printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno));
return 1; return 1;
} }
if (chown(outname, source_to_use->st.st_uid, if (chown(outname, source_to_use->st.st_uid,
source_to_use->st.st_gid) != 0) { source_to_use->st.st_gid) != 0) {
fprintf(stderr, "chown of \"%s\" failed: %s\n", outname, strerror(errno)); printf("chown of \"%s\" failed: %s\n", outname, strerror(errno));
return 1; return 1;
} }
// Finally, rename the .patch file to replace the target file. // Finally, rename the .patch file to replace the target file.
if (rename(outname, target_filename) != 0) { if (rename(outname, target_filename) != 0) {
fprintf(stderr, "rename of .patch to \"%s\" failed: %s\n", printf("rename of .patch to \"%s\" failed: %s\n",
target_filename, strerror(errno)); target_filename, strerror(errno));
return 1; return 1;
} }

View file

@ -16,7 +16,7 @@ static int EliminateOpenFiles(char** files, int file_count) {
struct dirent* de; struct dirent* de;
d = opendir("/proc"); d = opendir("/proc");
if (d == NULL) { if (d == NULL) {
fprintf(stderr, "error opening /proc: %s\n", strerror(errno)); printf("error opening /proc: %s\n", strerror(errno));
return -1; return -1;
} }
while ((de = readdir(d)) != 0) { while ((de = readdir(d)) != 0) {
@ -35,7 +35,7 @@ static int EliminateOpenFiles(char** files, int file_count) {
struct dirent* fdde; struct dirent* fdde;
fdd = opendir(path); fdd = opendir(path);
if (fdd == NULL) { if (fdd == NULL) {
fprintf(stderr, "error opening %s: %s\n", path, strerror(errno)); printf("error opening %s: %s\n", path, strerror(errno));
continue; continue;
} }
while ((fdde = readdir(fdd)) != 0) { while ((fdde = readdir(fdd)) != 0) {
@ -88,7 +88,7 @@ int FindExpendableFiles(char*** names, int* entries) {
for (i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) { for (i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) {
d = opendir(dirs[i]); d = opendir(dirs[i]);
if (d == NULL) { if (d == NULL) {
fprintf(stderr, "error opening %s: %s\n", dirs[i], strerror(errno)); printf("error opening %s: %s\n", dirs[i], strerror(errno));
continue; continue;
} }
@ -143,7 +143,7 @@ int MakeFreeSpaceOnCache(size_t bytes_needed) {
if (entries == 0) { if (entries == 0) {
// nothing we can delete to free up space! // nothing we can delete to free up space!
fprintf(stderr, "no files can be deleted to free space on /cache\n"); printf("no files can be deleted to free space on /cache\n");
return -1; return -1;
} }

View file

@ -179,14 +179,14 @@ unsigned char* ReadZip(const char* filename,
int include_pseudo_chunk) { int include_pseudo_chunk) {
struct stat st; struct stat st;
if (stat(filename, &st) != 0) { if (stat(filename, &st) != 0) {
fprintf(stderr, "failed to stat \"%s\": %s\n", filename, strerror(errno)); printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
return NULL; return NULL;
} }
unsigned char* img = malloc(st.st_size); unsigned char* img = malloc(st.st_size);
FILE* f = fopen(filename, "rb"); FILE* f = fopen(filename, "rb");
if (fread(img, 1, st.st_size, f) != st.st_size) { if (fread(img, 1, st.st_size, f) != st.st_size) {
fprintf(stderr, "failed to read \"%s\" %s\n", filename, strerror(errno)); printf("failed to read \"%s\" %s\n", filename, strerror(errno));
fclose(f); fclose(f);
return NULL; return NULL;
} }
@ -203,7 +203,7 @@ unsigned char* ReadZip(const char* filename,
} }
// double-check: this archive consists of a single "disk" // double-check: this archive consists of a single "disk"
if (!(img[i+4] == 0 && img[i+5] == 0 && img[i+6] == 0 && img[i+7] == 0)) { if (!(img[i+4] == 0 && img[i+5] == 0 && img[i+6] == 0 && img[i+7] == 0)) {
fprintf(stderr, "can't process multi-disk archive\n"); printf("can't process multi-disk archive\n");
return NULL; return NULL;
} }
@ -216,7 +216,7 @@ unsigned char* ReadZip(const char* filename,
unsigned char* cd = img+cdoffset; unsigned char* cd = img+cdoffset;
for (i = 0; i < cdcount; ++i) { for (i = 0; i < cdcount; ++i) {
if (!(cd[0] == 0x50 && cd[1] == 0x4b && cd[2] == 0x01 && cd[3] == 0x02)) { if (!(cd[0] == 0x50 && cd[1] == 0x4b && cd[2] == 0x01 && cd[3] == 0x02)) {
fprintf(stderr, "bad central directory entry %d\n", i); printf("bad central directory entry %d\n", i);
return NULL; return NULL;
} }
@ -243,12 +243,12 @@ unsigned char* ReadZip(const char* filename,
unsigned char* lh = img + hoffset; unsigned char* lh = img + hoffset;
if (!(lh[0] == 0x50 && lh[1] == 0x4b && lh[2] == 0x03 && lh[3] == 0x04)) { if (!(lh[0] == 0x50 && lh[1] == 0x4b && lh[2] == 0x03 && lh[3] == 0x04)) {
fprintf(stderr, "bad local file header entry %d\n", i); printf("bad local file header entry %d\n", i);
return NULL; return NULL;
} }
if (Read2(lh+26) != nlen || memcmp(lh+30, filename, nlen) != 0) { if (Read2(lh+26) != nlen || memcmp(lh+30, filename, nlen) != 0) {
fprintf(stderr, "central dir filename doesn't match local header\n"); printf("central dir filename doesn't match local header\n");
return NULL; return NULL;
} }
@ -320,7 +320,7 @@ unsigned char* ReadZip(const char* filename,
strm.next_out = curr->data; strm.next_out = curr->data;
ret = inflate(&strm, Z_NO_FLUSH); ret = inflate(&strm, Z_NO_FLUSH);
if (ret != Z_STREAM_END) { if (ret != Z_STREAM_END) {
fprintf(stderr, "failed to inflate \"%s\"; %d\n", curr->filename, ret); printf("failed to inflate \"%s\"; %d\n", curr->filename, ret);
return NULL; return NULL;
} }
@ -369,14 +369,14 @@ unsigned char* ReadImage(const char* filename,
int* num_chunks, ImageChunk** chunks) { int* num_chunks, ImageChunk** chunks) {
struct stat st; struct stat st;
if (stat(filename, &st) != 0) { if (stat(filename, &st) != 0) {
fprintf(stderr, "failed to stat \"%s\": %s\n", filename, strerror(errno)); printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
return NULL; return NULL;
} }
unsigned char* img = malloc(st.st_size + 4); unsigned char* img = malloc(st.st_size + 4);
FILE* f = fopen(filename, "rb"); FILE* f = fopen(filename, "rb");
if (fread(img, 1, st.st_size, f) != st.st_size) { if (fread(img, 1, st.st_size, f) != st.st_size) {
fprintf(stderr, "failed to read \"%s\" %s\n", filename, strerror(errno)); printf("failed to read \"%s\" %s\n", filename, strerror(errno));
fclose(f); fclose(f);
return NULL; return NULL;
} }
@ -476,7 +476,7 @@ unsigned char* ReadImage(const char* filename,
// the decompression. // the decompression.
size_t footer_size = Read4(p-4); size_t footer_size = Read4(p-4);
if (footer_size != curr[-2].len) { if (footer_size != curr[-2].len) {
fprintf(stderr, "Error: footer size %d != decompressed size %d\n", printf("Error: footer size %d != decompressed size %d\n",
footer_size, curr[-2].len); footer_size, curr[-2].len);
free(img); free(img);
return NULL; return NULL;
@ -522,7 +522,7 @@ int TryReconstruction(ImageChunk* chunk, unsigned char* out) {
size_t p = 0; size_t p = 0;
#if 0 #if 0
fprintf(stderr, "trying %d %d %d %d %d\n", printf("trying %d %d %d %d %d\n",
chunk->level, chunk->method, chunk->windowBits, chunk->level, chunk->method, chunk->windowBits,
chunk->memLevel, chunk->strategy); chunk->memLevel, chunk->strategy);
#endif #endif
@ -565,7 +565,7 @@ int TryReconstruction(ImageChunk* chunk, unsigned char* out) {
*/ */
int ReconstructDeflateChunk(ImageChunk* chunk) { int ReconstructDeflateChunk(ImageChunk* chunk) {
if (chunk->type != CHUNK_DEFLATE) { if (chunk->type != CHUNK_DEFLATE) {
fprintf(stderr, "attempt to reconstruct non-deflate chunk\n"); printf("attempt to reconstruct non-deflate chunk\n");
return -1; return -1;
} }
@ -610,13 +610,13 @@ unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) {
int r = bsdiff(src->data, src->len, &(src->I), tgt->data, tgt->len, ptemp); int r = bsdiff(src->data, src->len, &(src->I), tgt->data, tgt->len, ptemp);
if (r != 0) { if (r != 0) {
fprintf(stderr, "bsdiff() failed: %d\n", r); printf("bsdiff() failed: %d\n", r);
return NULL; return NULL;
} }
struct stat st; struct stat st;
if (stat(ptemp, &st) != 0) { if (stat(ptemp, &st) != 0) {
fprintf(stderr, "failed to stat patch file %s: %s\n", printf("failed to stat patch file %s: %s\n",
ptemp, strerror(errno)); ptemp, strerror(errno));
return NULL; return NULL;
} }
@ -635,11 +635,11 @@ unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) {
FILE* f = fopen(ptemp, "rb"); FILE* f = fopen(ptemp, "rb");
if (f == NULL) { if (f == NULL) {
fprintf(stderr, "failed to open patch %s: %s\n", ptemp, strerror(errno)); printf("failed to open patch %s: %s\n", ptemp, strerror(errno));
return NULL; return NULL;
} }
if (fread(data, 1, st.st_size, f) != st.st_size) { if (fread(data, 1, st.st_size, f) != st.st_size) {
fprintf(stderr, "failed to read patch %s: %s\n", ptemp, strerror(errno)); printf("failed to read patch %s: %s\n", ptemp, strerror(errno));
return NULL; return NULL;
} }
fclose(f); fclose(f);
@ -691,7 +691,7 @@ int AreChunksEqual(ImageChunk* a, ImageChunk* b) {
memcmp(a->deflate_data, b->deflate_data, a->deflate_len) == 0; memcmp(a->deflate_data, b->deflate_data, a->deflate_len) == 0;
default: default:
fprintf(stderr, "unknown chunk type %d\n", a->type); printf("unknown chunk type %d\n", a->type);
return 0; return 0;
} }
} }
@ -774,7 +774,7 @@ void DumpChunks(ImageChunk* chunks, int num_chunks) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
if (argc != 4 && argc != 5) { if (argc != 4 && argc != 5) {
usage: usage:
fprintf(stderr, "usage: %s [-z] <src-img> <tgt-img> <patch-file>\n", printf("usage: %s [-z] <src-img> <tgt-img> <patch-file>\n",
argv[0]); argv[0]);
return 2; return 2;
} }
@ -796,20 +796,20 @@ int main(int argc, char** argv) {
if (zip_mode) { if (zip_mode) {
if (ReadZip(argv[1], &num_src_chunks, &src_chunks, 1) == NULL) { if (ReadZip(argv[1], &num_src_chunks, &src_chunks, 1) == NULL) {
fprintf(stderr, "failed to break apart source zip file\n"); printf("failed to break apart source zip file\n");
return 1; return 1;
} }
if (ReadZip(argv[2], &num_tgt_chunks, &tgt_chunks, 0) == NULL) { if (ReadZip(argv[2], &num_tgt_chunks, &tgt_chunks, 0) == NULL) {
fprintf(stderr, "failed to break apart target zip file\n"); printf("failed to break apart target zip file\n");
return 1; return 1;
} }
} else { } else {
if (ReadImage(argv[1], &num_src_chunks, &src_chunks) == NULL) { if (ReadImage(argv[1], &num_src_chunks, &src_chunks) == NULL) {
fprintf(stderr, "failed to break apart source image\n"); printf("failed to break apart source image\n");
return 1; return 1;
} }
if (ReadImage(argv[2], &num_tgt_chunks, &tgt_chunks) == NULL) { if (ReadImage(argv[2], &num_tgt_chunks, &tgt_chunks) == NULL) {
fprintf(stderr, "failed to break apart target image\n"); printf("failed to break apart target image\n");
return 1; return 1;
} }
@ -824,7 +824,7 @@ int main(int argc, char** argv) {
} }
if (num_src_chunks != num_tgt_chunks) { if (num_src_chunks != num_tgt_chunks) {
fprintf(stderr, "source and target don't have same number of chunks!\n"); printf("source and target don't have same number of chunks!\n");
printf("source chunks:\n"); printf("source chunks:\n");
DumpChunks(src_chunks, num_src_chunks); DumpChunks(src_chunks, num_src_chunks);
printf("target chunks:\n"); printf("target chunks:\n");
@ -833,7 +833,7 @@ int main(int argc, char** argv) {
} }
for (i = 0; i < num_src_chunks; ++i) { for (i = 0; i < num_src_chunks; ++i) {
if (src_chunks[i].type != tgt_chunks[i].type) { if (src_chunks[i].type != tgt_chunks[i].type) {
fprintf(stderr, "source and target don't have same chunk " printf("source and target don't have same chunk "
"structure! (chunk %d)\n", i); "structure! (chunk %d)\n", i);
printf("source chunks:\n"); printf("source chunks:\n");
DumpChunks(src_chunks, num_src_chunks); DumpChunks(src_chunks, num_src_chunks);
@ -901,7 +901,7 @@ int main(int argc, char** argv) {
MergeAdjacentNormalChunks(src_chunks, &num_src_chunks); MergeAdjacentNormalChunks(src_chunks, &num_src_chunks);
if (num_src_chunks != num_tgt_chunks) { if (num_src_chunks != num_tgt_chunks) {
// This shouldn't happen. // This shouldn't happen.
fprintf(stderr, "merging normal chunks went awry\n"); printf("merging normal chunks went awry\n");
return 1; return 1;
} }
} }

View file

@ -40,13 +40,13 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
SinkFn sink, void* token, SHA_CTX* ctx) { SinkFn sink, void* token, SHA_CTX* ctx) {
FILE* f; FILE* f;
if ((f = fopen(patch_filename, "rb")) == NULL) { if ((f = fopen(patch_filename, "rb")) == NULL) {
fprintf(stderr, "failed to open patch file\n"); printf("failed to open patch file\n");
return -1; return -1;
} }
unsigned char header[12]; unsigned char header[12];
if (fread(header, 1, 12, f) != 12) { if (fread(header, 1, 12, f) != 12) {
fprintf(stderr, "failed to read patch file header\n"); printf("failed to read patch file header\n");
return -1; return -1;
} }
@ -54,7 +54,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW. // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
if (memcmp(header, "IMGDIFF", 7) != 0 || if (memcmp(header, "IMGDIFF", 7) != 0 ||
(header[7] != '1' && header[7] != '2')) { (header[7] != '1' && header[7] != '2')) {
fprintf(stderr, "corrupt patch file header (magic number)\n"); printf("corrupt patch file header (magic number)\n");
return -1; return -1;
} }
@ -65,7 +65,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// each chunk's header record starts with 4 bytes. // each chunk's header record starts with 4 bytes.
unsigned char chunk[4]; unsigned char chunk[4];
if (fread(chunk, 1, 4, f) != 4) { if (fread(chunk, 1, 4, f) != 4) {
fprintf(stderr, "failed to read chunk %d record\n", i); printf("failed to read chunk %d record\n", i);
return -1; return -1;
} }
@ -74,7 +74,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
if (type == CHUNK_NORMAL) { if (type == CHUNK_NORMAL) {
unsigned char normal_header[24]; unsigned char normal_header[24];
if (fread(normal_header, 1, 24, f) != 24) { if (fread(normal_header, 1, 24, f) != 24) {
fprintf(stderr, "failed to read chunk %d normal header data\n", i); printf("failed to read chunk %d normal header data\n", i);
return -1; return -1;
} }
@ -82,7 +82,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
size_t src_len = Read8(normal_header+8); size_t src_len = Read8(normal_header+8);
size_t patch_offset = Read8(normal_header+16); size_t patch_offset = Read8(normal_header+16);
fprintf(stderr, "CHUNK %d: normal patch offset %d\n", i, patch_offset); printf("CHUNK %d: normal patch offset %d\n", i, patch_offset);
ApplyBSDiffPatch(old_data + src_start, src_len, ApplyBSDiffPatch(old_data + src_start, src_len,
patch_filename, patch_offset, patch_filename, patch_offset,
@ -98,14 +98,14 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// in their chunk header. // in their chunk header.
unsigned char* gzip = malloc(64); unsigned char* gzip = malloc(64);
if (fread(gzip, 1, 64, f) != 64) { if (fread(gzip, 1, 64, f) != 64) {
fprintf(stderr, "failed to read chunk %d initial gzip header data\n", printf("failed to read chunk %d initial gzip header data\n",
i); i);
return -1; return -1;
} }
size_t gzip_header_len = Read4(gzip+60); size_t gzip_header_len = Read4(gzip+60);
gzip = realloc(gzip, 64 + gzip_header_len + 8); gzip = realloc(gzip, 64 + gzip_header_len + 8);
if (fread(gzip+64, 1, gzip_header_len+8, f) != gzip_header_len+8) { if (fread(gzip+64, 1, gzip_header_len+8, f) != gzip_header_len+8) {
fprintf(stderr, "failed to read chunk %d remaining gzip header data\n", printf("failed to read chunk %d remaining gzip header data\n",
i); i);
return -1; return -1;
} }
@ -122,14 +122,14 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
int gz_memLevel = Read4(gzip+52); int gz_memLevel = Read4(gzip+52);
int gz_strategy = Read4(gzip+56); int gz_strategy = Read4(gzip+56);
fprintf(stderr, "CHUNK %d: gzip patch offset %d\n", i, patch_offset); printf("CHUNK %d: gzip patch offset %d\n", i, patch_offset);
// Decompress the source data; the chunk header tells us exactly // Decompress the source data; the chunk header tells us exactly
// how big we expect it to be when decompressed. // how big we expect it to be when decompressed.
unsigned char* expanded_source = malloc(expanded_len); unsigned char* expanded_source = malloc(expanded_len);
if (expanded_source == NULL) { if (expanded_source == NULL) {
fprintf(stderr, "failed to allocate %d bytes for expanded_source\n", printf("failed to allocate %d bytes for expanded_source\n",
expanded_len); expanded_len);
return -1; return -1;
} }
@ -146,7 +146,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
int ret; int ret;
ret = inflateInit2(&strm, -15); ret = inflateInit2(&strm, -15);
if (ret != Z_OK) { if (ret != Z_OK) {
fprintf(stderr, "failed to init source inflation: %d\n", ret); printf("failed to init source inflation: %d\n", ret);
return -1; return -1;
} }
@ -154,12 +154,12 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// data, we expect one call to inflate() to suffice. // data, we expect one call to inflate() to suffice.
ret = inflate(&strm, Z_SYNC_FLUSH); ret = inflate(&strm, Z_SYNC_FLUSH);
if (ret != Z_STREAM_END) { if (ret != Z_STREAM_END) {
fprintf(stderr, "source inflation returned %d\n", ret); printf("source inflation returned %d\n", ret);
return -1; return -1;
} }
// We should have filled the output buffer exactly. // We should have filled the output buffer exactly.
if (strm.avail_out != 0) { if (strm.avail_out != 0) {
fprintf(stderr, "source inflation short by %d bytes\n", strm.avail_out); printf("source inflation short by %d bytes\n", strm.avail_out);
return -1; return -1;
} }
inflateEnd(&strm); inflateEnd(&strm);
@ -208,7 +208,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
size_t have = temp_size - strm.avail_out; size_t have = temp_size - strm.avail_out;
if (sink(temp_data, have, token) != have) { if (sink(temp_data, have, token) != have) {
fprintf(stderr, "failed to write %d compressed bytes to output\n", printf("failed to write %d compressed bytes to output\n",
have); have);
return -1; return -1;
} }
@ -226,29 +226,29 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
} else if (type == CHUNK_RAW) { } else if (type == CHUNK_RAW) {
unsigned char raw_header[4]; unsigned char raw_header[4];
if (fread(raw_header, 1, 4, f) != 4) { if (fread(raw_header, 1, 4, f) != 4) {
fprintf(stderr, "failed to read chunk %d raw header data\n", i); printf("failed to read chunk %d raw header data\n", i);
return -1; return -1;
} }
size_t data_len = Read4(raw_header); size_t data_len = Read4(raw_header);
fprintf(stderr, "CHUNK %d: raw data %d\n", i, data_len); printf("CHUNK %d: raw data %d\n", i, data_len);
unsigned char* temp = malloc(data_len); unsigned char* temp = malloc(data_len);
if (fread(temp, 1, data_len, f) != data_len) { if (fread(temp, 1, data_len, f) != data_len) {
fprintf(stderr, "failed to read chunk %d raw data\n", i); printf("failed to read chunk %d raw data\n", i);
return -1; return -1;
} }
SHA_update(ctx, temp, data_len); SHA_update(ctx, temp, data_len);
if (sink(temp, data_len, token) != data_len) { if (sink(temp, data_len, token) != data_len) {
fprintf(stderr, "failed to write chunk %d raw data\n", i); printf("failed to write chunk %d raw data\n", i);
return -1; return -1;
} }
} else if (type == CHUNK_DEFLATE) { } else if (type == CHUNK_DEFLATE) {
// deflate chunks have an additional 60 bytes in their chunk header. // deflate chunks have an additional 60 bytes in their chunk header.
unsigned char deflate_header[60]; unsigned char deflate_header[60];
if (fread(deflate_header, 1, 60, f) != 60) { if (fread(deflate_header, 1, 60, f) != 60) {
fprintf(stderr, "failed to read chunk %d deflate header data\n", i); printf("failed to read chunk %d deflate header data\n", i);
return -1; return -1;
} }
@ -263,14 +263,14 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
int memLevel = Read4(deflate_header+52); int memLevel = Read4(deflate_header+52);
int strategy = Read4(deflate_header+56); int strategy = Read4(deflate_header+56);
fprintf(stderr, "CHUNK %d: deflate patch offset %d\n", i, patch_offset); printf("CHUNK %d: deflate patch offset %d\n", i, patch_offset);
// Decompress the source data; the chunk header tells us exactly // Decompress the source data; the chunk header tells us exactly
// how big we expect it to be when decompressed. // how big we expect it to be when decompressed.
unsigned char* expanded_source = malloc(expanded_len); unsigned char* expanded_source = malloc(expanded_len);
if (expanded_source == NULL) { if (expanded_source == NULL) {
fprintf(stderr, "failed to allocate %d bytes for expanded_source\n", printf("failed to allocate %d bytes for expanded_source\n",
expanded_len); expanded_len);
return -1; return -1;
} }
@ -287,7 +287,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
int ret; int ret;
ret = inflateInit2(&strm, -15); ret = inflateInit2(&strm, -15);
if (ret != Z_OK) { if (ret != Z_OK) {
fprintf(stderr, "failed to init source inflation: %d\n", ret); printf("failed to init source inflation: %d\n", ret);
return -1; return -1;
} }
@ -295,12 +295,12 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// data, we expect one call to inflate() to suffice. // data, we expect one call to inflate() to suffice.
ret = inflate(&strm, Z_SYNC_FLUSH); ret = inflate(&strm, Z_SYNC_FLUSH);
if (ret != Z_STREAM_END) { if (ret != Z_STREAM_END) {
fprintf(stderr, "source inflation returned %d\n", ret); printf("source inflation returned %d\n", ret);
return -1; return -1;
} }
// We should have filled the output buffer exactly. // We should have filled the output buffer exactly.
if (strm.avail_out != 0) { if (strm.avail_out != 0) {
fprintf(stderr, "source inflation short by %d bytes\n", strm.avail_out); printf("source inflation short by %d bytes\n", strm.avail_out);
return -1; return -1;
} }
inflateEnd(&strm); inflateEnd(&strm);
@ -344,7 +344,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
size_t have = temp_size - strm.avail_out; size_t have = temp_size - strm.avail_out;
if (sink(temp_data, have, token) != have) { if (sink(temp_data, have, token) != have) {
fprintf(stderr, "failed to write %d compressed bytes to output\n", printf("failed to write %d compressed bytes to output\n",
have); have);
return -1; return -1;
} }
@ -355,7 +355,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
free(temp_data); free(temp_data);
free(uncompressed_target_data); free(uncompressed_target_data);
} else { } else {
fprintf(stderr, "patch chunk %d is unknown type %d\n", i, type); printf("patch chunk %d is unknown type %d\n", i, type);
return -1; return -1;
} }
} }

View file

@ -44,7 +44,7 @@ extern int applypatch(int argc, char** argv);
int main(int argc, char** argv) { int main(int argc, char** argv) {
int result = applypatch(argc, argv); int result = applypatch(argc, argv);
if (result == 2) { if (result == 2) {
fprintf(stderr, printf(
"usage: %s <src-file> <tgt-file> <tgt-sha1> <tgt-size> " "usage: %s <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
"[<src-sha1>:<patch> ...]\n" "[<src-sha1>:<patch> ...]\n"
" or %s -c <file> [<sha1> ...]\n" " or %s -c <file> [<sha1> ...]\n"