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

This commit is contained in:
Sarah Zakarias
2018-01-15 11:09:01 +01:00
committed by GitHub
parent 2c53613b1f
commit a50c855b12
2 changed files with 39 additions and 5 deletions

View File

@@ -7,21 +7,48 @@
#include <fcntl.h>
#if !defined(OS_WIN)
#include <unistd.h>
#endif
#include <utility>
#include "lib/fxl/files/eintr_wrapper.h"
#include "lib/fxl/files/file.h"
#include "lib/fxl/files/path.h"
#include "lib/fxl/files/unique_fd.h"
#include "lib/fxl/portable_unistd.h"
namespace blink {
bool DirectoryAssetBundle::GetAsBuffer(const std::string& asset_name,
std::vector<uint8_t>* data) {
if (fd_.is_valid()) {
#if defined(OS_WIN)
// This code path is not valid in a Windows environment.
return false;
#else
fxl::UniqueFD asset_file(openat(fd_.get(), asset_name.c_str(), O_RDONLY));
if (!asset_file.is_valid()) {
FXL_LOG(ERROR) << "Could not load asset " << asset_name;
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;
#endif
}
std::string asset_path = GetPathForAsset(asset_name);
if (asset_path.empty())
return false;
@@ -31,7 +58,10 @@ bool DirectoryAssetBundle::GetAsBuffer(const std::string& asset_name,
DirectoryAssetBundle::~DirectoryAssetBundle() {}
DirectoryAssetBundle::DirectoryAssetBundle(std::string directory)
: directory_(std::move(directory)) {}
: directory_(std::move(directory)), fd_() {}
DirectoryAssetBundle::DirectoryAssetBundle(fxl::UniqueFD fd)
: 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);
~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);
};