summaryrefslogtreecommitdiff
path: root/cmds/idmap2/libidmap2/Idmap.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cmds/idmap2/libidmap2/Idmap.cpp')
-rw-r--r--cmds/idmap2/libidmap2/Idmap.cpp109
1 files changed, 43 insertions, 66 deletions
diff --git a/cmds/idmap2/libidmap2/Idmap.cpp b/cmds/idmap2/libidmap2/Idmap.cpp
index 1129413584b2..5af84b0c6e4e 100644
--- a/cmds/idmap2/libidmap2/Idmap.cpp
+++ b/cmds/idmap2/libidmap2/Idmap.cpp
@@ -69,38 +69,26 @@ bool WARN_UNUSED Read32(std::istream& stream, uint32_t* out) {
return false;
}
-// a string is encoded as a kIdmapStringLength char array; the array is always null-terminated
-bool WARN_UNUSED ReadString256(std::istream& stream, char out[kIdmapStringLength]) {
- char buf[kIdmapStringLength];
- memset(buf, 0, sizeof(buf));
- if (!stream.read(buf, sizeof(buf))) {
- return false;
- }
- if (buf[sizeof(buf) - 1] != '\0') {
- return false;
- }
- memcpy(out, buf, sizeof(buf));
- return true;
-}
-
-Result<std::string> ReadString(std::istream& stream) {
+bool WARN_UNUSED ReadString(std::istream& stream, std::string* out) {
uint32_t size;
if (!Read32(stream, &size)) {
- return Error("failed to read string size");
+ return false;
}
if (size == 0) {
- return std::string("");
+ *out = "";
+ return true;
}
std::string buf(size, '\0');
if (!stream.read(buf.data(), size)) {
- return Error("failed to read string of size %u", size);
+ return false;
}
uint32_t padding_size = CalculatePadding(size);
std::string padding(padding_size, '\0');
if (!stream.read(padding.data(), padding_size)) {
- return Error("failed to read string padding of size %u", padding_size);
+ return false;
}
- return buf;
+ *out = buf;
+ return true;
}
} // namespace
@@ -119,28 +107,25 @@ std::unique_ptr<const IdmapHeader> IdmapHeader::FromBinaryStream(std::istream& s
if (!Read32(stream, &idmap_header->magic_) || !Read32(stream, &idmap_header->version_) ||
!Read32(stream, &idmap_header->target_crc_) || !Read32(stream, &idmap_header->overlay_crc_) ||
!Read32(stream, &idmap_header->fulfilled_policies_) ||
- !Read32(stream, &enforce_overlayable) || !ReadString256(stream, idmap_header->target_path_) ||
- !ReadString256(stream, idmap_header->overlay_path_)) {
+ !Read32(stream, &enforce_overlayable) || !ReadString(stream, &idmap_header->target_path_) ||
+ !ReadString(stream, &idmap_header->overlay_path_) ||
+ !ReadString(stream, &idmap_header->overlay_name_) ||
+ !ReadString(stream, &idmap_header->debug_info_)) {
return nullptr;
}
idmap_header->enforce_overlayable_ = enforce_overlayable != 0U;
-
- auto debug_str = ReadString(stream);
- if (!debug_str) {
- return nullptr;
- }
- idmap_header->debug_info_ = std::move(*debug_str);
-
return std::move(idmap_header);
}
-Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overlay_path,
+Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
+ const std::string& overlay_path,
+ const std::string& overlay_name,
PolicyBitmask fulfilled_policies,
bool enforce_overlayable) const {
const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_path);
if (!target_zip) {
- return Error("failed to open target %s", target_path);
+ return Error("failed to open target %s", target_path.c_str());
}
const Result<uint32_t> target_crc = GetPackageCrc(*target_zip);
@@ -150,7 +135,7 @@ Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overla
const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_path);
if (!overlay_zip) {
- return Error("failed to overlay target %s", overlay_path);
+ return Error("failed to overlay target %s", overlay_path.c_str());
}
const Result<uint32_t> overlay_crc = GetPackageCrc(*overlay_zip);
@@ -158,13 +143,14 @@ Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overla
return Error("failed to get overlay crc");
}
- return IsUpToDate(target_path, overlay_path, *target_crc, *overlay_crc, fulfilled_policies,
- enforce_overlayable);
+ return IsUpToDate(target_path, overlay_path, overlay_name, *target_crc, *overlay_crc,
+ fulfilled_policies, enforce_overlayable);
}
-Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overlay_path,
- uint32_t target_crc, uint32_t overlay_crc,
- PolicyBitmask fulfilled_policies,
+Result<Unit> IdmapHeader::IsUpToDate(const std::string& target_path,
+ const std::string& overlay_path,
+ const std::string& overlay_name, uint32_t target_crc,
+ uint32_t overlay_crc, PolicyBitmask fulfilled_policies,
bool enforce_overlayable) const {
if (magic_ != kIdmapMagic) {
return Error("bad magic: actual 0x%08x, expected 0x%08x", magic_, kIdmapMagic);
@@ -194,14 +180,19 @@ Result<Unit> IdmapHeader::IsUpToDate(const char* target_path, const char* overla
enforce_overlayable ? "true" : "false", enforce_overlayable_ ? "true" : "false");
}
- if (strcmp(target_path, target_path_) != 0) {
- return Error("bad target path: idmap version %s, file system version %s", target_path,
- target_path_);
+ if (target_path != target_path_) {
+ return Error("bad target path: idmap version %s, file system version %s", target_path.c_str(),
+ target_path_.c_str());
+ }
+
+ if (overlay_path != overlay_path_) {
+ return Error("bad overlay path: idmap version %s, file system version %s", overlay_path.c_str(),
+ overlay_path_.c_str());
}
- if (strcmp(overlay_path, overlay_path_) != 0) {
- return Error("bad overlay path: idmap version %s, file system version %s", overlay_path,
- overlay_path_);
+ if (overlay_name != overlay_name_) {
+ return Error("bad overlay name: idmap version %s, file system version %s", overlay_name.c_str(),
+ overlay_name_.c_str());
}
return Unit{};
@@ -262,12 +253,9 @@ std::unique_ptr<const IdmapData> IdmapData::FromBinaryStream(std::istream& strea
}
// Read raw string pool bytes.
- auto string_pool_data = ReadString(stream);
- if (!string_pool_data) {
+ if (!ReadString(stream, &data->string_pool_data_)) {
return nullptr;
}
- data->string_pool_data_ = std::move(*string_pool_data);
-
return std::move(data);
}
@@ -337,6 +325,7 @@ Result<std::unique_ptr<const IdmapData>> IdmapData::FromResourceMapping(
Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const ApkAssets& target_apk_assets,
const ApkAssets& overlay_apk_assets,
+ const std::string& overlay_name,
const PolicyBitmask& fulfilled_policies,
bool enforce_overlayable) {
SYSTRACE << "Idmap::FromApkAssets";
@@ -368,32 +357,20 @@ Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const ApkAssets& targe
return Error(crc.GetError(), "failed to get zip CRC for overlay");
}
header->overlay_crc_ = *crc;
-
header->fulfilled_policies_ = fulfilled_policies;
header->enforce_overlayable_ = enforce_overlayable;
+ header->target_path_ = target_apk_path;
+ header->overlay_path_ = overlay_apk_path;
+ header->overlay_name_ = overlay_name;
- if (target_apk_path.size() > sizeof(header->target_path_)) {
- return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
- sizeof(header->target_path_));
- }
- memset(header->target_path_, 0, sizeof(header->target_path_));
- memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
-
- if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
- return Error("overlay apk path \"%s\" longer than maximum size %zu", overlay_apk_path.c_str(),
- sizeof(header->target_path_));
- }
- memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
- memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
-
- auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
- if (!overlay_info) {
- return overlay_info.GetError();
+ auto info = utils::ExtractOverlayManifestInfo(overlay_apk_path, overlay_name);
+ if (!info) {
+ return info.GetError();
}
LogInfo log_info;
auto resource_mapping =
- ResourceMapping::FromApkAssets(target_apk_assets, overlay_apk_assets, *overlay_info,
+ ResourceMapping::FromApkAssets(target_apk_assets, overlay_apk_assets, *info,
fulfilled_policies, enforce_overlayable, log_info);
if (!resource_mapping) {
return resource_mapping.GetError();