summaryrefslogtreecommitdiff
path: root/tools/aapt2/optimize/ResourcePathShortener.cpp
diff options
context:
space:
mode:
authorMohamed Heikal <mheikal@google.com>2019-07-17 17:47:17 -0400
committerMohamed Heikal <mheikal@google.com>2019-07-30 15:52:38 -0400
commitb2bac12ce882fe1402aa06f395cfc867367c07be (patch)
tree365301a1a8ad746e066f675227c3ffc4c8ec109d /tools/aapt2/optimize/ResourcePathShortener.cpp
parentc7487391b0870e59a5c9d44d8e479bb55984ac5f (diff)
ResourcePathShortener deterministically handles hash collisions
ResourcePathShortener appends one digit at the end of colliding short file names to disambiguate them. This cl sorts the list of file paths so that colliding files always get the same disambiguating digit from one run to the next rather than possibly alternating. Test: make aapt2_tests Change-Id: I983a1448c21f2c79d7cdb1de232e7758c04fc256
Diffstat (limited to 'tools/aapt2/optimize/ResourcePathShortener.cpp')
-rw-r--r--tools/aapt2/optimize/ResourcePathShortener.cpp12
1 files changed, 11 insertions, 1 deletions
diff --git a/tools/aapt2/optimize/ResourcePathShortener.cpp b/tools/aapt2/optimize/ResourcePathShortener.cpp
index 7f5d10475f93..7ff9bf5aa8df 100644
--- a/tools/aapt2/optimize/ResourcePathShortener.cpp
+++ b/tools/aapt2/optimize/ResourcePathShortener.cpp
@@ -16,6 +16,7 @@
#include "optimize/ResourcePathShortener.h"
+#include <set>
#include <unordered_set>
#include "androidfw/StringPiece.h"
@@ -71,10 +72,19 @@ std::string GetShortenedPath(const android::StringPiece& shortened_filename,
return shortened_path;
}
+// implement custom comparator of FileReference pointers so as to use the
+// underlying filepath as key rather than the integer address. This is to ensure
+// determinism of output for colliding files.
+struct PathComparator {
+ bool operator() (const FileReference* lhs, const FileReference* rhs) const {
+ return lhs->path->compare(*rhs->path);
+ }
+};
+
bool ResourcePathShortener::Consume(IAaptContext* context, ResourceTable* table) {
// used to detect collisions
std::unordered_set<std::string> shortened_paths;
- std::unordered_set<FileReference*> file_refs;
+ std::set<FileReference*, PathComparator> file_refs;
for (auto& package : table->packages) {
for (auto& type : package->types) {
for (auto& entry : type->entries) {