Skip to content

Commit

Permalink
libmbpatcher: Fix incorrect add_file() overload being called
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed Jun 13, 2018
1 parent d7def58 commit 81fccc0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 127 deletions.
7 changes: 1 addition & 6 deletions libmbpatcher/include/mbpatcher/private/fileutils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2016 Andrew Gunnerson <[email protected]>
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand All @@ -20,7 +20,6 @@
#pragma once

#include <string>
#include <vector>

#include "mbcommon/file/standard.h"

Expand All @@ -37,13 +36,9 @@ class FileUtils
const std::string &path,
FileOpenMode mode);

static ErrorCode read_to_memory(const std::string &path,
std::vector<unsigned char> *contents);
static ErrorCode read_to_string(const std::string &path,
std::string *contents);

static ErrorCode write_from_memory(const std::string &path,
const std::vector<unsigned char> &contents);
static ErrorCode write_from_string(const std::string &path,
const std::string &contents);

Expand Down
15 changes: 7 additions & 8 deletions libmbpatcher/include/mbpatcher/private/miniziputils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,19 @@ class MinizipUtils
const std::string &name,
const std::function<void(uint64_t bytes)> &cb);

static bool read_to_memory(void *handle,
std::vector<unsigned char> &output,
static bool read_to_memory(void *handle, std::string &output,
const std::function<void(uint64_t bytes)> &cb);

static bool extract_file(void *handle,
const std::string &directory);

static ErrorCode add_file(void *handle,
const std::string &name,
const std::vector<unsigned char> &contents);
static ErrorCode add_file_from_data(void *handle,
const std::string &name,
const std::string &data);

static ErrorCode add_file(void *handle,
const std::string &name,
const std::string &path);
static ErrorCode add_file_from_path(void *handle,
const std::string &name,
const std::string &path);
};

}
14 changes: 6 additions & 8 deletions libmbpatcher/src/patchers/odinpatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ bool OdinPatcher::patch_tar()

update_details(spec.target);

result = MinizipUtils::add_file(handle, spec.target, spec.source);
result = MinizipUtils::add_file_from_path(
handle, spec.target, spec.source);
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand All @@ -272,11 +273,9 @@ bool OdinPatcher::patch_tar()

update_details("multiboot/info.prop");

const std::string info_prop =
ZipPatcher::create_info_prop(m_info->rom_id());
result = MinizipUtils::add_file(
result = MinizipUtils::add_file_from_data(
handle, "multiboot/info.prop",
std::vector<unsigned char>(info_prop.begin(), info_prop.end()));
ZipPatcher::create_info_prop(m_info->rom_id()));
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand All @@ -292,9 +291,8 @@ bool OdinPatcher::patch_tar()
return false;
}

result = MinizipUtils::add_file(
handle, "multiboot/device.json",
std::vector<unsigned char>(json.begin(), json.end()));
result = MinizipUtils::add_file_from_data(
handle, "multiboot/device.json", json);
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand Down
21 changes: 8 additions & 13 deletions libmbpatcher/src/patchers/ramdiskupdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ bool RamdiskUpdater::create_zip()
for (const CopySpec &spec : toCopy) {
if (m_cancelled) return false;

result = MinizipUtils::add_file(handle, spec.target, spec.source);
result = MinizipUtils::add_file_from_path(
handle, spec.target, spec.source);
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand All @@ -164,11 +165,9 @@ bool RamdiskUpdater::create_zip()

if (m_cancelled) return false;

const std::string info_prop =
ZipPatcher::create_info_prop(m_info->rom_id());
result = MinizipUtils::add_file(
result = MinizipUtils::add_file_from_data(
handle, "multiboot/info.prop",
std::vector<unsigned char>(info_prop.begin(), info_prop.end()));
ZipPatcher::create_info_prop(m_info->rom_id()));
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand All @@ -182,9 +181,8 @@ bool RamdiskUpdater::create_zip()
return false;
}

result = MinizipUtils::add_file(
handle, "multiboot/device.json",
std::vector<unsigned char>(json.begin(), json.end()));
result = MinizipUtils::add_file_from_data(
handle, "multiboot/device.json", json);
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand All @@ -193,12 +191,9 @@ bool RamdiskUpdater::create_zip()
if (m_cancelled) return false;

// Create dummy "installer"
std::string installer("#!/sbin/sh");

result = MinizipUtils::add_file(
result = MinizipUtils::add_file_from_data(
handle, "META-INF/com/google/android/update-binary.orig",
std::vector<unsigned char>(installer.begin(), installer.end()));

"#!/sbin/sh");
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand Down
18 changes: 8 additions & 10 deletions libmbpatcher/src/patchers/zippatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ bool ZipPatcher::patch_zip()
update_files(++m_files, m_max_files);
update_details(spec.target);

result = MinizipUtils::add_file(handle, spec.target, spec.source);
result = MinizipUtils::add_file_from_path(
handle, spec.target, spec.source);
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand All @@ -264,11 +265,9 @@ bool ZipPatcher::patch_zip()
update_files(++m_files, m_max_files);
update_details("multiboot/info.prop");

const std::string info_prop =
ZipPatcher::create_info_prop(m_info->rom_id());
result = MinizipUtils::add_file(
result = MinizipUtils::add_file_from_data(
handle, "multiboot/info.prop",
std::vector<unsigned char>(info_prop.begin(), info_prop.end()));
create_info_prop(m_info->rom_id()));
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand All @@ -285,9 +284,8 @@ bool ZipPatcher::patch_zip()
return false;
}

result = MinizipUtils::add_file(
handle, "multiboot/device.json",
std::vector<unsigned char>(json.begin(), json.end()));
result = MinizipUtils::add_file_from_data(
handle, "multiboot/device.json", json);
if (result != ErrorCode::NoError) {
m_error = result;
return false;
Expand Down Expand Up @@ -401,12 +399,12 @@ bool ZipPatcher::pass2(const std::string &temporary_dir,
ErrorCode ret;

if (file == "META-INF/com/google/android/update-binary") {
ret = MinizipUtils::add_file(
ret = MinizipUtils::add_file_from_path(
handle,
"META-INF/com/google/android/update-binary.orig",
temporary_dir + "/" + file);
} else {
ret = MinizipUtils::add_file(
ret = MinizipUtils::add_file_from_path(
handle,
file,
temporary_dir + "/" + file);
Expand Down
71 changes: 1 addition & 70 deletions libmbpatcher/src/private/fileutils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand Down Expand Up @@ -60,53 +60,6 @@ oc::result<void> FileUtils::open_file(StandardFile &file,
#endif
}

/*!
* \brief Read contents of a file into memory
*
* \param path Path to file
* \param contents Output vector (not modified unless reading succeeds)
*
* \return Success or not
*/
ErrorCode FileUtils::read_to_memory(const std::string &path,
std::vector<unsigned char> *contents)
{
StandardFile file;

auto ret = open_file(file, path, FileOpenMode::ReadOnly);
if (!ret) {
LOGE("%s: Failed to open for reading: %s",
path.c_str(), ret.error().message().c_str());
return ErrorCode::FileOpenError;
}

auto size = file.seek(0, SEEK_END);
if (!size) {
LOGE("%s: Failed to seek file: %s",
path.c_str(), size.error().message().c_str());
return ErrorCode::FileSeekError;
}
auto seek_ret = file.seek(0, SEEK_SET);
if (!seek_ret) {
LOGE("%s: Failed to seek file: %s",
path.c_str(), seek_ret.error().message().c_str());
return ErrorCode::FileSeekError;
}

std::vector<unsigned char> data(static_cast<size_t>(size.value()));

auto bytes_read = file.read(data.data(), data.size());
if (!bytes_read || bytes_read.value() != size.value()) {
LOGE("%s: Failed to read file: %s",
path.c_str(), bytes_read.error().message().c_str());
return ErrorCode::FileReadError;
}

data.swap(*contents);

return ErrorCode::NoError;
}

/*!
* \brief Read contents of a file into a string
*
Expand Down Expand Up @@ -155,28 +108,6 @@ ErrorCode FileUtils::read_to_string(const std::string &path,
return ErrorCode::NoError;
}

ErrorCode FileUtils::write_from_memory(const std::string &path,
const std::vector<unsigned char> &contents)
{
StandardFile file;

auto ret = open_file(file, path, FileOpenMode::WriteOnly);
if (!ret) {
LOGE("%s: Failed to open for writing: %s",
path.c_str(), ret.error().message().c_str());
return ErrorCode::FileOpenError;
}

auto bytes_written = file.write(contents.data(), contents.size());
if (!bytes_written || bytes_written.value() != contents.size()) {
LOGE("%s: Failed to write file: %s",
path.c_str(), bytes_written.error().message().c_str());
return ErrorCode::FileWriteError;
}

return ErrorCode::NoError;
}

ErrorCode FileUtils::write_from_string(const std::string &path,
const std::string &contents)
{
Expand Down
23 changes: 11 additions & 12 deletions libmbpatcher/src/private/miniziputils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ bool MinizipUtils::copy_file_raw(void *source_handle,
return true;
}

bool MinizipUtils::read_to_memory(void *handle,
std::vector<unsigned char> &output,
bool MinizipUtils::read_to_memory(void *handle, std::string &output,
const std::function<void(uint64_t bytes)> &cb)
{
mz_zip_file *file_info;
Expand All @@ -310,7 +309,7 @@ bool MinizipUtils::read_to_memory(void *handle,
return false;
}

std::vector<unsigned char> data;
std::string data;
data.reserve(static_cast<size_t>(file_info->uncompressed_size));

ret = mz_zip_entry_read_open(handle, 0, nullptr);
Expand Down Expand Up @@ -428,9 +427,9 @@ bool MinizipUtils::extract_file(void *handle, const std::string &directory)
return n == 0;
}

ErrorCode MinizipUtils::add_file(void *handle,
const std::string &name,
const std::vector<unsigned char> &contents)
ErrorCode MinizipUtils::add_file_from_data(void *handle,
const std::string &name,
const std::string &data)
{
mz_zip_file file_info = {};
file_info.compression_method = MZ_COMPRESS_METHOD_DEFLATE;
Expand All @@ -449,9 +448,9 @@ ErrorCode MinizipUtils::add_file(void *handle,
});

// Write data to file
int n = mz_zip_entry_write(handle, contents.data(),
static_cast<uint32_t>(contents.size()));
if (n < 0 || static_cast<size_t>(n) != contents.size()) {
int n = mz_zip_entry_write(handle, data.data(),
static_cast<uint32_t>(data.size()));
if (n < 0 || static_cast<size_t>(n) != data.size()) {
LOGE("minizip: Failed to write inner file data");
return ErrorCode::ArchiveWriteDataError;
}
Expand All @@ -467,9 +466,9 @@ ErrorCode MinizipUtils::add_file(void *handle,
return ErrorCode::NoError;
}

ErrorCode MinizipUtils::add_file(void *handle,
const std::string &name,
const std::string &path)
ErrorCode MinizipUtils::add_file_from_path(void *handle,
const std::string &name,
const std::string &path)
{
// Copy file into archive
StandardFile file;
Expand Down

0 comments on commit 81fccc0

Please sign in to comment.