AAPT2: Compile --zip flag
Added a --zip flag similar to --dir that allows resources to be passed
into "aapt2 compile" using a zip file.
Also refactored Compile.cpp to be easier to mock and test in the future.
Bug: 74574557
Test: aapt2_tests
Change-Id: Idb90cb97e23a219525bdead38220cbf7bc6f3cab
diff --git a/tools/aapt2/io/FileSystem.cpp b/tools/aapt2/io/FileSystem.cpp
index 1387d22..16a20f4c 100644
--- a/tools/aapt2/io/FileSystem.cpp
+++ b/tools/aapt2/io/FileSystem.cpp
@@ -16,6 +16,9 @@
#include "io/FileSystem.h"
+#include <dirent.h>
+
+#include "android-base/errors.h"
#include "androidfw/StringPiece.h"
#include "utils/FileMap.h"
@@ -26,6 +29,7 @@
#include "util/Util.h"
using ::android::StringPiece;
+using ::android::base::SystemErrorCodeToString;
namespace aapt {
namespace io {
@@ -64,6 +68,50 @@
return result;
}
+std::unique_ptr<FileCollection> FileCollection::Create(const android::StringPiece& root,
+ std::string* outError) {
+ std::unique_ptr<FileCollection> collection =
+ std::unique_ptr<FileCollection>(new FileCollection());
+
+ std::unique_ptr<DIR, decltype(closedir) *> d(opendir(root.data()), closedir);
+ if (!d) {
+ *outError = "failed to open directory: " + SystemErrorCodeToString(errno);
+ return nullptr;
+ }
+
+ while (struct dirent *entry = readdir(d.get())) {
+ std::string prefix_path = root.to_string();
+ file::AppendPath(&prefix_path, entry->d_name);
+
+ // The directory to iterate over looking for files
+ if (file::GetFileType(prefix_path) != file::FileType::kDirectory
+ || file::IsHidden(prefix_path)) {
+ continue;
+ }
+
+ std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
+ if (!subdir) {
+ *outError = "failed to open directory: " + SystemErrorCodeToString(errno);
+ return nullptr;
+ }
+
+ while (struct dirent* leaf_entry = readdir(subdir.get())) {
+ std::string full_path = prefix_path;
+ file::AppendPath(&full_path, leaf_entry->d_name);
+
+ // Do not add folders to the file collection
+ if (file::GetFileType(full_path) == file::FileType::kDirectory
+ || file::IsHidden(full_path)) {
+ continue;
+ }
+
+ collection->InsertFile(full_path);
+ }
+ }
+
+ return collection;
+}
+
IFile* FileCollection::InsertFile(const StringPiece& path) {
return (files_[path.to_string()] = util::make_unique<RegularFile>(Source(path))).get();
}
diff --git a/tools/aapt2/io/FileSystem.h b/tools/aapt2/io/FileSystem.h
index 6be8807..fb6bf6e 100644
--- a/tools/aapt2/io/FileSystem.h
+++ b/tools/aapt2/io/FileSystem.h
@@ -59,6 +59,10 @@
public:
FileCollection() = default;
+ /** Creates a file collection containing all files contained in the specified root directory. */
+ static std::unique_ptr<FileCollection> Create(const android::StringPiece& path,
+ std::string* outError);
+
// Adds a file located at path. Returns the IFile representation of that file.
IFile* InsertFile(const android::StringPiece& path);
IFile* FindFile(const android::StringPiece& path) override;
diff --git a/tools/aapt2/io/ZipArchive.cpp b/tools/aapt2/io/ZipArchive.cpp
index 269b6c5..8e6d713 100644
--- a/tools/aapt2/io/ZipArchive.cpp
+++ b/tools/aapt2/io/ZipArchive.cpp
@@ -20,6 +20,7 @@
#include "ziparchive/zip_archive.h"
#include "Source.h"
+#include "util/Files.h"
#include "util/Util.h"
using ::android::StringPiece;
@@ -121,9 +122,14 @@
std::string zip_entry_path =
std::string(reinterpret_cast<const char*>(zip_entry_name.name),
zip_entry_name.name_length);
- std::string nested_path = path.to_string() + "@" + zip_entry_path;
- std::unique_ptr<IFile> file =
- util::make_unique<ZipFile>(collection->handle_, zip_data, Source(nested_path));
+
+ // Do not add folders to the file collection
+ if (util::EndsWith(zip_entry_path, "/")) {
+ continue;
+ }
+
+ std::unique_ptr<IFile> file = util::make_unique<ZipFile>(collection->handle_, zip_data,
+ Source(zip_entry_path, path.to_string()));
collection->files_by_name_[zip_entry_path] = file.get();
collection->files_.push_back(std::move(file));
}
@@ -132,6 +138,7 @@
if (out_error) *out_error = ErrorCodeString(result);
return {};
}
+
return collection;
}