summaryrefslogtreecommitdiffstats
path: root/applypatch/applypatch.cpp
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2017-02-01 19:20:10 +0100
committerTao Bao <tbao@google.com>2017-03-28 19:14:53 +0200
commitc0e1c46a707370952ea1ddeb71b176d04fe71bb9 (patch)
treeee0958c7ee3dfbcb545fa285715d0ac81896661e /applypatch/applypatch.cpp
parentapplypatch: Change the ssize_t length parameters to size_t. (diff)
downloadandroid_bootable_recovery-c0e1c46a707370952ea1ddeb71b176d04fe71bb9.tar
android_bootable_recovery-c0e1c46a707370952ea1ddeb71b176d04fe71bb9.tar.gz
android_bootable_recovery-c0e1c46a707370952ea1ddeb71b176d04fe71bb9.tar.bz2
android_bootable_recovery-c0e1c46a707370952ea1ddeb71b176d04fe71bb9.tar.lz
android_bootable_recovery-c0e1c46a707370952ea1ddeb71b176d04fe71bb9.tar.xz
android_bootable_recovery-c0e1c46a707370952ea1ddeb71b176d04fe71bb9.tar.zst
android_bootable_recovery-c0e1c46a707370952ea1ddeb71b176d04fe71bb9.zip
Diffstat (limited to 'applypatch/applypatch.cpp')
-rw-r--r--applypatch/applypatch.cpp24
1 files changed, 10 insertions, 14 deletions
diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp
index 06b8e3120..51bf3932a 100644
--- a/applypatch/applypatch.cpp
+++ b/applypatch/applypatch.cpp
@@ -27,6 +27,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include <functional>
#include <memory>
#include <string>
#include <utility>
@@ -42,7 +43,7 @@
#include "print_sha1.h"
static int LoadPartitionContents(const std::string& filename, FileContents* file);
-static size_t FileSink(const unsigned char* data, size_t len, void* token);
+static size_t FileSink(const unsigned char* data, size_t len, int fd);
static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
const std::string& target_filename,
const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data);
@@ -194,7 +195,7 @@ int SaveFileContents(const char* filename, const FileContents* file) {
return -1;
}
- size_t bytes_written = FileSink(file->data.data(), file->data.size(), &fd);
+ size_t bytes_written = FileSink(file->data.data(), file->data.size(), fd);
if (bytes_written != file->data.size()) {
printf("short write of \"%s\" (%zd bytes of %zu): %s\n", filename, bytes_written,
file->data.size(), strerror(errno));
@@ -433,8 +434,7 @@ int ShowLicenses() {
return 0;
}
-static size_t FileSink(const unsigned char* data, size_t len, void* token) {
- int fd = *static_cast<int*>(token);
+static size_t FileSink(const unsigned char* data, size_t len, int fd) {
size_t done = 0;
while (done < len) {
ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done));
@@ -447,12 +447,6 @@ static size_t FileSink(const unsigned char* data, size_t len, void* token) {
return done;
}
-size_t MemorySink(const unsigned char* data, size_t len, void* token) {
- std::string* s = static_cast<std::string*>(token);
- s->append(reinterpret_cast<const char*>(data), len);
- return len;
-}
-
// Return the amount of free space (in bytes) on the filesystem
// containing filename. filename must exist. Return -1 on error.
size_t FreeSpaceForFile(const char* filename) {
@@ -646,9 +640,11 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr
}
// We store the decoded output in memory.
- SinkFn sink = MemorySink;
std::string memory_sink_str; // Don't need to reserve space.
- void* token = &memory_sink_str;
+ SinkFn sink = [&memory_sink_str](const unsigned char* data, size_t len) {
+ memory_sink_str.append(reinterpret_cast<const char*>(data), len);
+ return len;
+ };
SHA_CTX ctx;
SHA1_Init(&ctx);
@@ -656,10 +652,10 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr
int result;
if (use_bsdiff) {
result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), patch.get(), 0,
- sink, token, &ctx);
+ sink, &ctx);
} else {
result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), patch.get(), sink,
- token, &ctx, bonus_data);
+ &ctx, bonus_data);
}
if (result != 0) {