forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory_asset_bundle.cc
75 lines (61 loc) · 2.14 KB
/
directory_asset_bundle.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/assets/directory_asset_bundle.h"
#include "lib/fxl/build_config.h"
#include <fcntl.h>
#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())
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;
return files::ReadFileToVector(asset_path, data);
}
DirectoryAssetBundle::~DirectoryAssetBundle() {}
DirectoryAssetBundle::DirectoryAssetBundle(std::string directory)
: directory_(std::move(directory)), fd_() {}
DirectoryAssetBundle::DirectoryAssetBundle(fxl::UniqueFD fd)
: fd_(std::move(fd)) {}
std::string DirectoryAssetBundle::GetPathForAsset(
const std::string& asset_name) {
std::string asset_path = files::SimplifyPath(directory_ + "/" + asset_name);
if (asset_path.find(directory_) != 0u) {
FXL_LOG(ERROR) << "Asset name '" << asset_name
<< "' attempted to traverse outside asset bundle.";
return std::string();
}
return asset_path;
}
} // namespace blink