forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore archive support removed in flutter#5305. Codepush bundles use…
… zip. (flutter#5441)
- Loading branch information
Showing
4 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// 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/zip_asset_store.h" | ||
#include "lib/fxl/build_config.h" | ||
|
||
#include <fcntl.h> | ||
|
||
#if !defined(OS_WIN) | ||
#include <unistd.h> | ||
#endif | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "flutter/glue/trace_event.h" | ||
|
||
namespace blink { | ||
|
||
ZipAssetStore::ZipAssetStore(std::string file_path) | ||
: file_path_(std::move(file_path)) { | ||
BuildStatCache(); | ||
} | ||
|
||
ZipAssetStore::~ZipAssetStore() = default; | ||
|
||
zip::UniqueUnzipper ZipAssetStore::CreateUnzipper() const { | ||
return zip::UniqueUnzipper{::unzOpen2(file_path_.c_str(), nullptr)}; | ||
} | ||
|
||
// |blink::AssetResolver| | ||
bool ZipAssetStore::IsValid() const { | ||
return stat_cache_.size() > 0; | ||
} | ||
|
||
// |blink::AssetResolver| | ||
bool ZipAssetStore::GetAsBuffer(const std::string& asset_name, | ||
std::vector<uint8_t>* data) const { | ||
TRACE_EVENT0("flutter", "ZipAssetStore::GetAsBuffer"); | ||
auto found = stat_cache_.find(asset_name); | ||
|
||
if (found == stat_cache_.end()) { | ||
return false; | ||
} | ||
|
||
auto unzipper = CreateUnzipper(); | ||
|
||
if (!unzipper.is_valid()) { | ||
return false; | ||
} | ||
|
||
int result = UNZ_OK; | ||
|
||
result = unzGoToFilePos(unzipper.get(), &(found->second.file_pos)); | ||
if (result != UNZ_OK) { | ||
FXL_LOG(WARNING) << "unzGetCurrentFileInfo failed, error=" << result; | ||
return false; | ||
} | ||
|
||
result = unzOpenCurrentFile(unzipper.get()); | ||
if (result != UNZ_OK) { | ||
FXL_LOG(WARNING) << "unzOpenCurrentFile failed, error=" << result; | ||
return false; | ||
} | ||
|
||
data->resize(found->second.uncompressed_size); | ||
int total_read = 0; | ||
while (total_read < static_cast<int>(data->size())) { | ||
int bytes_read = unzReadCurrentFile( | ||
unzipper.get(), data->data() + total_read, data->size() - total_read); | ||
if (bytes_read <= 0) { | ||
return false; | ||
} | ||
total_read += bytes_read; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void ZipAssetStore::BuildStatCache() { | ||
TRACE_EVENT0("flutter", "ZipAssetStore::BuildStatCache"); | ||
|
||
auto unzipper = CreateUnzipper(); | ||
|
||
if (!unzipper.is_valid()) { | ||
return; | ||
} | ||
|
||
if (unzGoToFirstFile(unzipper.get()) != UNZ_OK) { | ||
return; | ||
} | ||
|
||
do { | ||
int result = UNZ_OK; | ||
|
||
// Get the current file name. | ||
unz_file_info file_info = {}; | ||
char file_name[255]; | ||
result = unzGetCurrentFileInfo(unzipper.get(), &file_info, file_name, | ||
sizeof(file_name), nullptr, 0, nullptr, 0); | ||
if (result != UNZ_OK) { | ||
continue; | ||
} | ||
|
||
if (file_info.uncompressed_size == 0) { | ||
continue; | ||
} | ||
|
||
// Get the current file position. | ||
unz_file_pos file_pos = {}; | ||
result = unzGetFilePos(unzipper.get(), &file_pos); | ||
if (result != UNZ_OK) { | ||
continue; | ||
} | ||
|
||
std::string file_name_key(file_name, file_info.size_filename); | ||
CacheEntry entry(file_pos, file_info.uncompressed_size); | ||
stat_cache_.emplace(std::move(file_name_key), std::move(entry)); | ||
|
||
} while (unzGoToNextFile(unzipper.get()) == UNZ_OK); | ||
} | ||
|
||
} // namespace blink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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. | ||
|
||
#ifndef FLUTTER_ASSETS_ZIP_ASSET_STORE_H_ | ||
#define FLUTTER_ASSETS_ZIP_ASSET_STORE_H_ | ||
|
||
#include <map> | ||
|
||
#include "flutter/assets/asset_resolver.h" | ||
#include "lib/fxl/macros.h" | ||
#include "lib/fxl/memory/ref_counted.h" | ||
#include "lib/zip/unique_unzipper.h" | ||
#include "third_party/zlib/contrib/minizip/unzip.h" | ||
|
||
namespace blink { | ||
|
||
class ZipAssetStore final : public AssetResolver { | ||
public: | ||
ZipAssetStore(std::string file_path); | ||
|
||
~ZipAssetStore() override; | ||
|
||
private: | ||
struct CacheEntry { | ||
unz_file_pos file_pos; | ||
size_t uncompressed_size; | ||
CacheEntry(unz_file_pos p_file_pos, size_t p_uncompressed_size) | ||
: file_pos(p_file_pos), uncompressed_size(p_uncompressed_size) {} | ||
}; | ||
|
||
std::string file_path_; | ||
mutable std::map<std::string, CacheEntry> stat_cache_; | ||
|
||
// |blink::AssetResolver| | ||
bool IsValid() const override; | ||
|
||
// |blink::AssetResolver| | ||
bool GetAsBuffer(const std::string& asset_name, | ||
std::vector<uint8_t>* data) const override; | ||
|
||
void BuildStatCache(); | ||
|
||
zip::UniqueUnzipper CreateUnzipper() const; | ||
|
||
FXL_DISALLOW_COPY_AND_ASSIGN(ZipAssetStore); | ||
}; | ||
|
||
} // namespace blink | ||
|
||
#endif // FLUTTER_ASSETS_ZIP_ASSET_STORE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters