summaryrefslogtreecommitdiff
path: root/tools/aapt2/io/FileSystem.cpp
diff options
context:
space:
mode:
authorRyan Mitchell <rtmitchell@google.com>2018-08-16 17:26:08 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2018-08-16 17:26:08 +0000
commitead275e19fc9ffbd751b3956a84e27844b97ffe7 (patch)
treef19915c04e52949cb29908ef1e0f6588ad98d766 /tools/aapt2/io/FileSystem.cpp
parenta60283fdd1f2c67259e8b0c10d88ecfb60a1328b (diff)
parentf3649d669059b924ce9eb3eb7909cbf0a2ed31a8 (diff)
Merge "AAPT2: Compile --zip flag"
Diffstat (limited to 'tools/aapt2/io/FileSystem.cpp')
-rw-r--r--tools/aapt2/io/FileSystem.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/tools/aapt2/io/FileSystem.cpp b/tools/aapt2/io/FileSystem.cpp
index 1387d2218ed4..16a20f4cb09d 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 @@ IFile* FileCollectionIterator::Next() {
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();
}