Run Fuchsia apps without flx (flutter/engine#4538)

This commit is contained in:
Sarah Zakarias
2018-01-12 13:31:54 +01:00
committed by GitHub
parent 73634e73bc
commit 9bf76142c1
2 changed files with 39 additions and 1 deletions

View File

@@ -23,15 +23,49 @@ namespace blink {
bool DirectoryAssetBundle::GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) {
std::string asset_path = GetPathForAsset(asset_name);
if (asset_path.empty())
return false;
if (fd_.is_valid()) {
fxl::UniqueFD asset_file(openat(fd_.get(), asset_path.c_str(), O_RDONLY));
if (!asset_file.is_valid()) {
FXL_LOG(ERROR) << "Could not load asset " << asset_name << " from "
<< asset_path;
return false;
}
constexpr size_t kBufferSize = 1 << 16;
size_t offset = 0;
ssize_t bytes_read = 0;
do {
offset += bytes_read;
data->resize(offset + kBufferSize);
bytes_read = read(asset_file.get(), &(*data)[offset], kBufferSize);
} while (bytes_read > 0);
if (bytes_read < 0) {
FXL_LOG(ERROR) << "Reading " << asset_name << " failed";
data->clear();
return false;
}
data->resize(offset + bytes_read);
return true;
}
return files::ReadFileToVector(asset_path, data);
}
DirectoryAssetBundle::~DirectoryAssetBundle() {}
DirectoryAssetBundle::DirectoryAssetBundle(std::string directory)
: directory_(std::move(directory)) {}
: directory_(std::move(directory)),
fd_(fxl::internal::UniqueFDTraits::InvalidValue()) {}
DirectoryAssetBundle::DirectoryAssetBundle(fxl::UniqueFD fd,
std::string directory)
: directory_(std::move(directory)), fd_(std::move(fd)) {}
std::string DirectoryAssetBundle::GetPathForAsset(
const std::string& asset_name) {

View File

@@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include "lib/fxl/files/unique_fd.h"
#include "lib/fxl/macros.h"
#include "lib/fxl/memory/ref_counted.h"
@@ -17,6 +18,8 @@ class DirectoryAssetBundle
: public fxl::RefCountedThreadSafe<DirectoryAssetBundle> {
public:
explicit DirectoryAssetBundle(std::string directory);
// Expects fd to be valid, otherwise the file descriptor is ignored.
explicit DirectoryAssetBundle(fxl::UniqueFD fd, std::string directory);
~DirectoryAssetBundle();
bool GetAsBuffer(const std::string& asset_name, std::vector<uint8_t>* data);
@@ -25,6 +28,7 @@ class DirectoryAssetBundle
private:
const std::string directory_;
fxl::UniqueFD fd_;
FXL_DISALLOW_COPY_AND_ASSIGN(DirectoryAssetBundle);
};