diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 844001143395e..46261ef6cd1a3 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1150,18 +1150,6 @@ FILE: ../../../flutter/third_party/tonic/dart_state.h FILE: ../../../flutter/third_party/tonic/dart_wrappable.cc FILE: ../../../flutter/third_party/tonic/dart_wrappable.h FILE: ../../../flutter/third_party/tonic/dart_wrapper_info.h -FILE: ../../../flutter/third_party/tonic/file_loader/file_loader.cc -FILE: ../../../flutter/third_party/tonic/file_loader/file_loader.h -FILE: ../../../flutter/third_party/tonic/file_loader/file_loader_fuchsia.cc -FILE: ../../../flutter/third_party/tonic/file_loader/file_loader_posix.cc -FILE: ../../../flutter/third_party/tonic/file_loader/file_loader_win.cc -FILE: ../../../flutter/third_party/tonic/filesystem/filesystem/eintr_wrapper.h -FILE: ../../../flutter/third_party/tonic/filesystem/filesystem/file.cc -FILE: ../../../flutter/third_party/tonic/filesystem/filesystem/file.h -FILE: ../../../flutter/third_party/tonic/filesystem/filesystem/path.h -FILE: ../../../flutter/third_party/tonic/filesystem/filesystem/path_posix.cc -FILE: ../../../flutter/third_party/tonic/filesystem/filesystem/path_win.cc -FILE: ../../../flutter/third_party/tonic/filesystem/filesystem/portable_unistd.h FILE: ../../../flutter/third_party/tonic/logging/dart_error.cc FILE: ../../../flutter/third_party/tonic/logging/dart_error.h FILE: ../../../flutter/third_party/tonic/logging/dart_invoke.cc diff --git a/runtime/dart_isolate.cc b/runtime/dart_isolate.cc index 9f41ed8b2b379..955120bc14b75 100644 --- a/runtime/dart_isolate.cc +++ b/runtime/dart_isolate.cc @@ -23,7 +23,6 @@ #include "third_party/tonic/dart_class_provider.h" #include "third_party/tonic/dart_message_handler.h" #include "third_party/tonic/dart_state.h" -#include "third_party/tonic/file_loader/file_loader.h" #include "third_party/tonic/logging/dart_invoke.h" #include "third_party/tonic/scopes/dart_api_scope.h" #include "third_party/tonic/scopes/dart_isolate_scope.h" diff --git a/runtime/dart_vm.cc b/runtime/dart_vm.cc index 3338b0952bd01..aab033ce49e8a 100644 --- a/runtime/dart_vm.cc +++ b/runtime/dart_vm.cc @@ -31,7 +31,6 @@ #include "third_party/tonic/converter/dart_converter.h" #include "third_party/tonic/dart_class_library.h" #include "third_party/tonic/dart_class_provider.h" -#include "third_party/tonic/file_loader/file_loader.h" #include "third_party/tonic/logging/dart_error.h" #include "third_party/tonic/scopes/dart_api_scope.h" #include "third_party/tonic/typed_data/typed_list.h" diff --git a/shell/platform/fuchsia/dart_runner/dart_component_controller.cc b/shell/platform/fuchsia/dart_runner/dart_component_controller.cc index 02f412353e706..4084f6210d944 100644 --- a/shell/platform/fuchsia/dart_runner/dart_component_controller.cc +++ b/shell/platform/fuchsia/dart_runner/dart_component_controller.cc @@ -313,11 +313,9 @@ bool DartComponentController::CreateIsolate( // Create the isolate from the snapshot. char* error = nullptr; - // TODO(dart_runner): Pass if we start using tonic's loader. - intptr_t namespace_fd = -1; // Freed in IsolateShutdownCallback. auto state = new std::shared_ptr(new tonic::DartState( - namespace_fd, [this](Dart_Handle result) { MessageEpilogue(result); })); + [this](Dart_Handle result) { MessageEpilogue(result); })); isolate_ = Dart_CreateIsolateGroup( url_.c_str(), label_.c_str(), isolate_snapshot_data, diff --git a/third_party/tonic/BUILD.gn b/third_party/tonic/BUILD.gn index 4114ac4da6214..4be898b52f8ea 100644 --- a/third_party/tonic/BUILD.gn +++ b/third_party/tonic/BUILD.gn @@ -37,7 +37,6 @@ source_set("tonic") { public_deps = [ "common", "converter", - "file_loader", "logging", "scopes", "typed_data", diff --git a/third_party/tonic/dart_state.cc b/third_party/tonic/dart_state.cc index a3430d87004d2..597d8bb5759e5 100644 --- a/third_party/tonic/dart_state.cc +++ b/third_party/tonic/dart_state.cc @@ -7,7 +7,6 @@ #include "tonic/converter/dart_converter.h" #include "tonic/dart_class_library.h" #include "tonic/dart_message_handler.h" -#include "tonic/file_loader/file_loader.h" namespace tonic { @@ -19,12 +18,10 @@ DartState::Scope::Scope(std::shared_ptr dart_state) DartState::Scope::~Scope() {} -DartState::DartState(int dirfd, - std::function message_epilogue) +DartState::DartState(std::function message_epilogue) : isolate_(nullptr), class_library_(new DartClassLibrary), message_handler_(new DartMessageHandler()), - file_loader_(new FileLoader(dirfd)), message_epilogue_(message_epilogue), has_set_return_code_(false) {} @@ -66,10 +63,4 @@ void DartState::SetReturnCodeCallback(std::function callback) { void DartState::DidSetIsolate() {} -Dart_Handle DartState::HandleLibraryTag(Dart_LibraryTag tag, - Dart_Handle library, - Dart_Handle url) { - return Current()->file_loader().HandleLibraryTag(tag, library, url); -} - } // namespace tonic diff --git a/third_party/tonic/dart_state.h b/third_party/tonic/dart_state.h index d2c6e03cb7cc9..4f91d57df5db6 100644 --- a/third_party/tonic/dart_state.h +++ b/third_party/tonic/dart_state.h @@ -37,8 +37,7 @@ class DartState : public std::enable_shared_from_this { DartApiScope api_scope_; }; - DartState(int dirfd = -1, - std::function message_epilogue = nullptr); + DartState(std::function message_epilogue = nullptr); virtual ~DartState(); static DartState* From(Dart_Isolate isolate); @@ -51,7 +50,6 @@ class DartState : public std::enable_shared_from_this { DartClassLibrary& class_library() { return *class_library_; } DartMessageHandler& message_handler() { return *message_handler_; } - FileLoader& file_loader() { return *file_loader_; } void MessageEpilogue(Dart_Handle message_result) { if (message_epilogue_) { @@ -72,7 +70,6 @@ class DartState : public std::enable_shared_from_this { Dart_Isolate isolate_; std::unique_ptr class_library_; std::unique_ptr message_handler_; - std::unique_ptr file_loader_; std::function message_epilogue_; std::function set_return_code_callback_; bool has_set_return_code_; diff --git a/third_party/tonic/file_loader/BUILD.gn b/third_party/tonic/file_loader/BUILD.gn deleted file mode 100644 index e350a447c438f..0000000000000 --- a/third_party/tonic/file_loader/BUILD.gn +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2013 The Flutter Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -source_set("file_loader") { - visibility = [ "../*" ] - - configs += [ "../:config" ] - - sources = [ - "file_loader.cc", - "file_loader.h", - ] - - if (is_win) { - sources += [ "file_loader_win.cc" ] - } else if (is_fuchsia) { - sources += [ "file_loader_fuchsia.cc" ] - } else { - sources += [ "file_loader_posix.cc" ] - } - - deps = [ - "../common", - "../converter", - "../filesystem", - "../parsers", - "//third_party/dart/runtime:dart_api", - ] -} diff --git a/third_party/tonic/file_loader/file_loader.cc b/third_party/tonic/file_loader/file_loader.cc deleted file mode 100644 index 88cc9343c76fa..0000000000000 --- a/third_party/tonic/file_loader/file_loader.cc +++ /dev/null @@ -1,239 +0,0 @@ -// Copyright 2013 The Flutter 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 "tonic/file_loader/file_loader.h" - -#include -#include -#include - -#include "filesystem/file.h" -#include "filesystem/path.h" -#include "filesystem/portable_unistd.h" -#include "tonic/common/macros.h" -#include "tonic/converter/dart_converter.h" -#include "tonic/parsers/packages_map.h" - -namespace tonic { -namespace { - -constexpr char kDartScheme[] = "dart:"; - -constexpr char kFileScheme[] = "file:"; -constexpr size_t kFileSchemeLength = sizeof(kFileScheme) - 1; - -constexpr char kPackageScheme[] = "package:"; -constexpr size_t kPackageSchemeLength = sizeof(kPackageScheme) - 1; - -// Extract the scheme prefix ('package:' or 'file:' from ) -std::string ExtractSchemePrefix(std::string url) { - if (url.find(kPackageScheme) == 0u) - return kPackageScheme; - if (url.find(kFileScheme) == 0u) - return kFileScheme; - return std::string(); -} - -// Extract the path from a package: or file: url. -std::string ExtractPath(std::string url) { - if (url.find(kPackageScheme) == 0u) - return url.substr(kPackageSchemeLength); - if (url.find(kFileScheme) == 0u) - return url.substr(kFileSchemeLength); - return url; -} - -} // namespace - -FileLoader::FileLoader(int dirfd) : dirfd_(dirfd) {} - -FileLoader::~FileLoader() { - for (auto kernel_buffer : kernel_buffers_) - free(kernel_buffer); - - if (dirfd_ >= 0) - close(dirfd_); -} - -std::string FileLoader::SanitizeURIEscapedCharacters(const std::string& str) { - std::string result; - result.reserve(str.size()); - for (std::string::size_type i = 0; i < str.size(); ++i) { - if (str[i] == '%') { - if (i > str.size() - 3 || !isxdigit(str[i + 1]) || !isxdigit(str[i + 2])) - return ""; - const std::string hex = str.substr(i + 1, 2); - const unsigned char c = strtoul(hex.c_str(), nullptr, 16); - if (!c) - return ""; - result += c; - i += 2; - } else { - result += str[i]; - } - } - return result; -} - -bool FileLoader::LoadPackagesMap(const std::string& packages) { - packages_ = packages; - std::string packages_source; - if (!ReadFileToString(packages_, &packages_source)) { - tonic::Log("error: Unable to load .packages file '%s'.", packages_.c_str()); - return false; - } - packages_map_.reset(new PackagesMap()); - std::string error; - if (!packages_map_->Parse(packages_source, &error)) { - tonic::Log("error: Unable to parse .packages file '%s'. %s", - packages_.c_str(), error.c_str()); - return false; - } - return true; -} - -std::string FileLoader::GetFilePathForPackageURL(std::string url) { - if (!packages_map_) - return std::string(); - TONIC_DCHECK(url.find(kPackageScheme) == 0u); - url = url.substr(kPackageSchemeLength); - - size_t slash = url.find(FileLoader::kPathSeparator); - if (slash == std::string::npos) - return std::string(); - std::string package = url.substr(0, slash); - std::string library_path = url.substr(slash + 1); - std::string package_path = packages_map_->Resolve(package); - if (package_path.empty()) - return std::string(); - if (package_path.find(FileLoader::kFileURLPrefix) == 0u) - return SanitizePath(package_path.substr(FileLoader::kFileURLPrefixLength) + - library_path); - return filesystem::GetDirectoryName(filesystem::AbsolutePath(packages_)) + - FileLoader::kPathSeparator + package_path + - FileLoader::kPathSeparator + library_path; -} - -Dart_Handle FileLoader::HandleLibraryTag(Dart_LibraryTag tag, - Dart_Handle library, - Dart_Handle url) { - TONIC_DCHECK(Dart_IsNull(library) || Dart_IsLibrary(library) || - Dart_IsString(library)); - TONIC_DCHECK(Dart_IsString(url)); - if (tag == Dart_kCanonicalizeUrl) - return CanonicalizeURL(library, url); - if (tag == Dart_kKernelTag) - return Kernel(url); - if (tag == Dart_kImportTag) - return Import(url); - return Dart_NewApiError("Unknown library tag."); -} - -Dart_Handle FileLoader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) { - std::string string = StdStringFromDart(url); - if (string.find(kDartScheme) == 0u) - return url; - if (string.find(kPackageScheme) == 0u) - return StdStringToDart(SanitizePath(string)); - if (string.find(kFileScheme) == 0u) - return StdStringToDart(SanitizePath(CanonicalizeFileURL(string))); - - std::string library_url = StdStringFromDart(Dart_LibraryUrl(library)); - std::string prefix = ExtractSchemePrefix(library_url); - std::string base_path = ExtractPath(library_url); - std::string simplified_path = - filesystem::SimplifyPath(filesystem::GetDirectoryName(base_path) + - FileLoader::kPathSeparator + string); - return StdStringToDart(SanitizePath(prefix + simplified_path)); -} - -std::string FileLoader::GetFilePathForURL(std::string url) { - if (url.find(kPackageScheme) == 0u) - return GetFilePathForPackageURL(std::move(url)); - if (url.find(kFileScheme) == 0u) - return GetFilePathForFileURL(std::move(url)); - return url; -} - -Dart_Handle FileLoader::FetchBytes(const std::string& url, - uint8_t*& buffer, - intptr_t& buffer_size) { - buffer = nullptr; - buffer_size = -1; - - std::string path = filesystem::SimplifyPath(GetFilePathForURL(url)); - if (path.empty()) { - std::string error_message = "error: Unable to read '" + url + "'."; - return Dart_NewUnhandledExceptionError( - Dart_NewStringFromCString(error_message.c_str())); - } - std::string absolute_path = filesystem::GetAbsoluteFilePath(path); - auto result = filesystem::ReadFileToBytes(absolute_path); - if (result.first == nullptr) { - std::string error_message = - "error: Unable to read '" + absolute_path + "'."; - return Dart_NewUnhandledExceptionError( - Dart_NewStringFromCString(error_message.c_str())); - } - buffer = result.first; - buffer_size = result.second; - return Dart_True(); -} - -Dart_Handle FileLoader::Import(Dart_Handle url) { - std::string url_string = StdStringFromDart(url); - uint8_t* buffer = nullptr; - intptr_t buffer_size = -1; - Dart_Handle result = FetchBytes(url_string, buffer, buffer_size); - if (Dart_IsError(result)) { - return result; - } - // The embedder must keep the buffer alive until isolate shutdown. - kernel_buffers_.push_back(buffer); - return Dart_LoadLibraryFromKernel(buffer, buffer_size); -} - -namespace { -void MallocFinalizer(void* isolate_callback_data, - Dart_WeakPersistentHandle handle, - void* peer) { - free(peer); -} -} // namespace - -Dart_Handle FileLoader::Kernel(Dart_Handle url) { - std::string url_string = StdStringFromDart(url); - uint8_t* buffer = nullptr; - intptr_t buffer_size = -1; - Dart_Handle result = FetchBytes(url_string, buffer, buffer_size); - if (Dart_IsError(result)) { - return result; - } - result = - Dart_NewExternalTypedData(Dart_TypedData_kUint8, buffer, buffer_size); - Dart_NewWeakPersistentHandle(result, buffer, buffer_size, MallocFinalizer); - return result; -} - -// This is invoked upon a reload request. -void FileLoader::SetPackagesUrl(Dart_Handle url) { - if (url == Dart_Null()) { - // No packages url specified. - LoadPackagesMap(packages()); - return; - } - const std::string& packages_url = StdStringFromDart(url); - LoadPackagesMap(packages_url); -} - -std::string FileLoader::GetFilePathForFileURL(std::string url) { - TONIC_DCHECK(url.find(FileLoader::kFileURLPrefix) == 0u); - return SanitizePath(url.substr(FileLoader::kFileURLPrefixLength)); -} - -std::string FileLoader::GetFileURLForPath(const std::string& path) { - return std::string(FileLoader::kFileURLPrefix) + path; -} - -} // namespace tonic diff --git a/third_party/tonic/file_loader/file_loader.h b/third_party/tonic/file_loader/file_loader.h deleted file mode 100644 index 978d370fb8347..0000000000000 --- a/third_party/tonic/file_loader/file_loader.h +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2013 The Flutter 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 LIB_TONIC_FILE_LOADER_FILE_LOADER_H_ -#define LIB_TONIC_FILE_LOADER_FILE_LOADER_H_ - -#include -#include -#include -#include - -#include "third_party/dart/runtime/include/dart_api.h" -#include "tonic/common/macros.h" -#include "tonic/parsers/packages_map.h" - -namespace tonic { - -class FileLoader { - public: - FileLoader(int dirfd = -1); - ~FileLoader(); - - bool LoadPackagesMap(const std::string& packages); - - // The path to the `.packages` file the packages map was loaded from. - const std::string& packages() const { return packages_; } - - Dart_Handle HandleLibraryTag(Dart_LibraryTag tag, - Dart_Handle library, - Dart_Handle url); - - Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url); - Dart_Handle Import(Dart_Handle url); - Dart_Handle Kernel(Dart_Handle url); - void SetPackagesUrl(Dart_Handle url); - - Dart_Handle FetchBytes(const std::string& url, - uint8_t*& buffer, - intptr_t& buffer_size); - - static const char kFileURLPrefix[]; - static const size_t kFileURLPrefixLength; - static const std::string kPathSeparator; - - private: - static std::string SanitizeURIEscapedCharacters(const std::string& str); - static std::string SanitizePath(const std::string& path); - static std::string CanonicalizeFileURL(const std::string& url); - - std::string GetFilePathForURL(std::string url); - std::string GetFilePathForPackageURL(std::string url); - std::string GetFilePathForFileURL(std::string url); - - std::string GetFileURLForPath(const std::string& path); - - bool ReadFileToString(const std::string& path, std::string* result); - std::pair ReadFileToBytes(const std::string& path); - - int dirfd_; - std::string packages_; - std::unique_ptr packages_map_; - std::vector kernel_buffers_; - - TONIC_DISALLOW_COPY_AND_ASSIGN(FileLoader); -}; - -} // namespace tonic - -#endif // LIB_TONIC_FILE_LOADER_FILE_LOADER_H_ diff --git a/third_party/tonic/file_loader/file_loader_fuchsia.cc b/third_party/tonic/file_loader/file_loader_fuchsia.cc deleted file mode 100644 index 808dccdb31e51..0000000000000 --- a/third_party/tonic/file_loader/file_loader_fuchsia.cc +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2013 The Flutter 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 "tonic/file_loader/file_loader.h" - -#include -#include -#include -#include - -#include -#include -#include - -#include "filesystem/file.h" -#include "filesystem/path.h" -#include "tonic/common/macros.h" -#include "tonic/converter/dart_converter.h" -#include "tonic/parsers/packages_map.h" - -namespace tonic { - -const std::string FileLoader::kPathSeparator = "/"; -const char FileLoader::kFileURLPrefix[] = "file://"; -const size_t FileLoader::kFileURLPrefixLength = - sizeof(FileLoader::kFileURLPrefix) - 1; - -namespace { - -const size_t kFileSchemeLength = FileLoader::kFileURLPrefixLength - 2; - -} // namespace - -std::string FileLoader::SanitizePath(const std::string& url) { - return SanitizeURIEscapedCharacters(url); -} - -std::string FileLoader::CanonicalizeFileURL(const std::string& url) { - return url.substr(kFileSchemeLength); -} - -bool FileLoader::ReadFileToString(const std::string& path, - std::string* result) { - if (dirfd_ == -1) - return filesystem::ReadFileToString(path, result); - const char* cpath = path.c_str(); - const int offset = (cpath[0] == '/') ? 1 : 0; - filesystem::Descriptor fd(openat(dirfd_, &cpath[offset], O_RDONLY)); - return filesystem::ReadFileDescriptorToString(fd.get(), result); -} - -std::pair FileLoader::ReadFileToBytes( - const std::string& path) { - if (dirfd_ == -1) - return filesystem::ReadFileToBytes(path); - const char* cpath = path.c_str(); - const int offset = (cpath[0] == '/') ? 1 : 0; - filesystem::Descriptor fd(openat(dirfd_, &cpath[offset], O_RDONLY)); - return filesystem::ReadFileDescriptorToBytes(fd.get()); -} - -} // namespace tonic diff --git a/third_party/tonic/file_loader/file_loader_posix.cc b/third_party/tonic/file_loader/file_loader_posix.cc deleted file mode 100644 index 13f3bd3caebd3..0000000000000 --- a/third_party/tonic/file_loader/file_loader_posix.cc +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2013 The Flutter 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 "tonic/file_loader/file_loader.h" - -#include -#include -#include - -#include "filesystem/file.h" -#include "filesystem/path.h" -#include "tonic/common/macros.h" -#include "tonic/converter/dart_converter.h" -#include "tonic/parsers/packages_map.h" - -namespace tonic { - -const std::string FileLoader::kPathSeparator = "/"; -const char FileLoader::kFileURLPrefix[] = "file://"; -const size_t FileLoader::kFileURLPrefixLength = - sizeof(FileLoader::kFileURLPrefix) - 1; - -namespace { - -const size_t kFileSchemeLength = FileLoader::kFileURLPrefixLength - 2; - -} // namespace - -std::string FileLoader::SanitizePath(const std::string& url) { - return SanitizeURIEscapedCharacters(url); -} - -std::string FileLoader::CanonicalizeFileURL(const std::string& url) { - return url.substr(kFileSchemeLength); -} - -bool FileLoader::ReadFileToString(const std::string& path, - std::string* result) { - TONIC_DCHECK(dirfd_ == -1); - return filesystem::ReadFileToString(path, result); -} - -std::pair FileLoader::ReadFileToBytes( - const std::string& path) { - TONIC_DCHECK(dirfd_ == -1); - return filesystem::ReadFileToBytes(path); -} - -} // namespace tonic diff --git a/third_party/tonic/file_loader/file_loader_win.cc b/third_party/tonic/file_loader/file_loader_win.cc deleted file mode 100644 index e79e86f1a0439..0000000000000 --- a/third_party/tonic/file_loader/file_loader_win.cc +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2013 The Flutter 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 "tonic/file_loader/file_loader.h" - -#include -#include -#include - -#include "filesystem/file.h" -#include "filesystem/path.h" -#include "tonic/common/macros.h" -#include "tonic/parsers/packages_map.h" - -namespace tonic { -namespace { - -void FindAndReplaceInPlace(std::string& str, - const std::string& findStr, - const std::string& replaceStr) { - size_t pos = 0; - while ((pos = str.find(findStr, pos)) != std::string::npos) { - str.replace(pos, findStr.length(), replaceStr); - pos += replaceStr.length(); - } -} - -} // namespace - -const char FileLoader::kFileURLPrefix[] = "file:///"; -const size_t FileLoader::kFileURLPrefixLength = - sizeof(FileLoader::kFileURLPrefix) - 1; -const std::string FileLoader::kPathSeparator = "\\"; - -std::string FileLoader::SanitizePath(const std::string& url) { - std::string sanitized = url; - FindAndReplaceInPlace(sanitized, "/", FileLoader::kPathSeparator); - return SanitizeURIEscapedCharacters(sanitized); -} - -std::string FileLoader::CanonicalizeFileURL(const std::string& url) { - return SanitizePath(url.substr(FileLoader::kFileURLPrefixLength)); -} - -bool FileLoader::ReadFileToString(const std::string& path, - std::string* result) { - TONIC_DCHECK(dirfd_ == -1); - return filesystem::ReadFileToString(path, result); -} - -std::pair FileLoader::ReadFileToBytes( - const std::string& path) { - TONIC_DCHECK(dirfd_ == -1); - return filesystem::ReadFileToBytes(path); -} - -} // namespace tonic diff --git a/third_party/tonic/filesystem/BUILD.gn b/third_party/tonic/filesystem/BUILD.gn deleted file mode 100644 index a8bbfd364c40c..0000000000000 --- a/third_party/tonic/filesystem/BUILD.gn +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright 2013 The Flutter Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -group("filesystem") { - public_deps = [ - "filesystem", - ] -} diff --git a/third_party/tonic/filesystem/README.md b/third_party/tonic/filesystem/README.md deleted file mode 100644 index a97e8f865fd7d..0000000000000 --- a/third_party/tonic/filesystem/README.md +++ /dev/null @@ -1,4 +0,0 @@ -Files -===== - -A simple cross-platform library with minimal dependencies to work with files and paths. \ No newline at end of file diff --git a/third_party/tonic/filesystem/filesystem/BUILD.gn b/third_party/tonic/filesystem/filesystem/BUILD.gn deleted file mode 100644 index 7b8cbd688fa12..0000000000000 --- a/third_party/tonic/filesystem/filesystem/BUILD.gn +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2013 The Flutter Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -config("filesystem_config") { - visibility = [ ":*" ] - - # We want callers to refer to headers in this folders using the "files" - # prefix. - include_dirs = [ ".." ] -} - -source_set("filesystem") { - visibility = [ "../*" ] - - configs += [ "../../:config" ] - - sources = [ - "eintr_wrapper.h", - "file.cc", - "file.h", - "path.h", - "portable_unistd.h", - ] - - if (is_win) { - sources += [ "path_win.cc" ] - } else { - sources += [ "path_posix.cc" ] - } - - deps = [ - "../../common", - ] - - public_configs = [ ":filesystem_config" ] -} diff --git a/third_party/tonic/filesystem/filesystem/eintr_wrapper.h b/third_party/tonic/filesystem/filesystem/eintr_wrapper.h deleted file mode 100644 index 183c813d0186a..0000000000000 --- a/third_party/tonic/filesystem/filesystem/eintr_wrapper.h +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2013 The Flutter 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 FILESYSTEM_EINTR_WRAPPER_H_ -#define FILESYSTEM_EINTR_WRAPPER_H_ - -#include - -#include "tonic/common/build_config.h" - -#if defined(OS_WIN) - -// Windows has no concept of EINTR. -#define HANDLE_EINTR(x) (x) -#define IGNORE_EINTR(x) (x) - -#else - -#if defined(NDEBUG) - -#define HANDLE_EINTR(x) \ - ({ \ - decltype(x) eintr_wrapper_result; \ - do { \ - eintr_wrapper_result = (x); \ - } while (eintr_wrapper_result == -1 && errno == EINTR); \ - eintr_wrapper_result; \ - }) - -#else - -#define HANDLE_EINTR(x) \ - ({ \ - int eintr_wrapper_counter = 0; \ - decltype(x) eintr_wrapper_result; \ - do { \ - eintr_wrapper_result = (x); \ - } while (eintr_wrapper_result == -1 && errno == EINTR && \ - eintr_wrapper_counter++ < 100); \ - eintr_wrapper_result; \ - }) - -#endif // NDEBUG - -#define IGNORE_EINTR(x) \ - ({ \ - decltype(x) eintr_wrapper_result; \ - do { \ - eintr_wrapper_result = (x); \ - if (eintr_wrapper_result == -1 && errno == EINTR) { \ - eintr_wrapper_result = 0; \ - } \ - } while (0); \ - eintr_wrapper_result; \ - }) - -#endif // defined(OS_WIN) - -#endif // FILESYSTEM_EINTR_WRAPPER_H_ diff --git a/third_party/tonic/filesystem/filesystem/file.cc b/third_party/tonic/filesystem/filesystem/file.cc deleted file mode 100644 index 53e8e053affb7..0000000000000 --- a/third_party/tonic/filesystem/filesystem/file.cc +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2013 The Flutter 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 "filesystem/file.h" - -#include -#include -#include -#include - -#include "tonic/common/build_config.h" - -#if defined(OS_WIN) -#define BINARY_MODE _O_BINARY -#else -#define BINARY_MODE 0 -#endif - -#if defined(OS_WIN) -#include -typedef SSIZE_T ssize_t; -#endif - -#include "filesystem/eintr_wrapper.h" -#include "filesystem/portable_unistd.h" - -namespace filesystem { -namespace { - -template -bool ReadFileDescriptor(int fd, T* result) { - if (!result) { - return false; - } - - result->clear(); - - if (fd < 0) - return false; - - constexpr size_t kBufferSize = 1 << 16; - size_t offset = 0; - ssize_t bytes_read = 0; - do { - offset += bytes_read; - result->resize(offset + kBufferSize); - bytes_read = HANDLE_EINTR(read(fd, &(*result)[offset], kBufferSize)); - } while (bytes_read > 0); - - if (bytes_read < 0) { - result->clear(); - return false; - } - - result->resize(offset + bytes_read); - return true; -} - -} // namespace - -std::pair ReadFileDescriptorToBytes(int fd) { - std::pair failure_pair{nullptr, -1}; - struct stat st; - if (fstat(fd, &st) != 0) { - return failure_pair; - } - intptr_t file_size = st.st_size; - uint8_t* ptr = (uint8_t*)malloc(file_size); - - size_t bytes_left = file_size; - size_t offset = 0; - while (bytes_left > 0) { - ssize_t bytes_read = HANDLE_EINTR(read(fd, &ptr[offset], bytes_left)); - if (bytes_read < 0) { - return failure_pair; - } - offset += bytes_read; - bytes_left -= bytes_read; - } - return std::pair(ptr, file_size); -} - -bool ReadFileToString(const std::string& path, std::string* result) { - Descriptor fd(open(path.c_str(), O_RDONLY)); - return ReadFileDescriptor(fd.get(), result); -} - -bool ReadFileDescriptorToString(int fd, std::string* result) { - return ReadFileDescriptor(fd, result); -} - -std::pair ReadFileToBytes(const std::string& path) { - std::pair failure_pair{nullptr, -1}; - Descriptor fd(open(path.c_str(), O_RDONLY | BINARY_MODE)); - if (!fd.is_valid()) - return failure_pair; - return ReadFileDescriptorToBytes(fd.get()); -} - -} // namespace filesystem diff --git a/third_party/tonic/filesystem/filesystem/file.h b/third_party/tonic/filesystem/filesystem/file.h deleted file mode 100644 index ee206850df50c..0000000000000 --- a/third_party/tonic/filesystem/filesystem/file.h +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2013 The Flutter 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 FILESYSTEM_FILE_H_ -#define FILESYSTEM_FILE_H_ - -#include -#include - -#include "filesystem/eintr_wrapper.h" -#include "filesystem/portable_unistd.h" - -namespace filesystem { - -class Descriptor { - public: - using Handle = int; - - Descriptor(Handle handle) : handle_(handle) {} - - ~Descriptor() { - if (is_valid()) { - IGNORE_EINTR(::close(handle_)); - } - } - - bool is_valid() { return handle_ >= 0; } - - Handle get() { return handle_; } - - private: - Handle handle_ = -1; - - Descriptor(Descriptor&) = delete; - - void operator=(const Descriptor&) = delete; -}; - -// Reads the contents of the file at the given path or file descriptor and -// stores the data in result. Returns true if the file was read successfully, -// otherwise returns false. If this function returns false, |result| will be -// the empty string. -bool ReadFileToString(const std::string& path, std::string* result); -bool ReadFileDescriptorToString(int fd, std::string* result); - -// Reads the contents of the file at the given path and if successful, returns -// pair of read allocated bytes with data and size of the data if succesful. -// pair of if read failed. -std::pair ReadFileToBytes(const std::string& path); -std::pair ReadFileDescriptorToBytes(int fd); - -} // namespace filesystem - -#endif // FILESYSTEM_FILE_H_ diff --git a/third_party/tonic/filesystem/filesystem/path.h b/third_party/tonic/filesystem/filesystem/path.h deleted file mode 100644 index 442186c24bc7b..0000000000000 --- a/third_party/tonic/filesystem/filesystem/path.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2013 The Flutter 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 FILESYSTEM_PATH_H_ -#define FILESYSTEM_PATH_H_ - -#include - -namespace filesystem { - -// Resolves ".." and "." components of the path syntactically without consulting -// the file system. -std::string SimplifyPath(std::string path); - -// Returns the absolute path of a possibly relative path. -// It doesn't consult the filesystem or simplify the path. -std::string AbsolutePath(const std::string& path); - -// Returns the directory name component of the given path. -std::string GetDirectoryName(const std::string& path); - -// Returns the basename component of the given path by stripping everything up -// to and including the last slash. -std::string GetBaseName(const std::string& path); - -// Returns the real path for the given path by unwinding symbolic links and -// directory traversals. -std::string GetAbsoluteFilePath(const std::string& path); - -} // namespace filesystem - -#endif // FILESYSTEM_PATH_H_ diff --git a/third_party/tonic/filesystem/filesystem/path_posix.cc b/third_party/tonic/filesystem/filesystem/path_posix.cc deleted file mode 100644 index 5c748251e4d51..0000000000000 --- a/third_party/tonic/filesystem/filesystem/path_posix.cc +++ /dev/null @@ -1,188 +0,0 @@ -// Copyright 2013 The Flutter 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 "filesystem/path.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "filesystem/portable_unistd.h" -#include "tonic/common/build_config.h" - -namespace filesystem { -namespace { - -size_t ResolveParentDirectoryTraversal(const std::string& path, size_t put) { - if (put >= 2) { - size_t previous_separator = path.rfind('/', put - 2); - if (previous_separator != std::string::npos) - return previous_separator + 1; - } - if (put == 1 && path[0] == '/') { - return put; - } - return 0; -} - -std::string GetCurrentDirectory() { - char buffer[PATH_MAX]; - if (getcwd(buffer, sizeof(buffer)) == NULL) { - return {}; - } - return std::string(buffer); -} - -} // namespace - -std::string SimplifyPath(std::string path) { - if (path.empty()) - return "."; - - size_t put = 0; - size_t get = 0; - size_t traversal_root = 0; - size_t component_start = 0; - - if (path[0] == '/') { - put = 1; - get = 1; - component_start = 1; - } - - while (get < path.size()) { - char c = path[get]; - - if (c == '.' && (get == component_start || get == component_start + 1)) { - // We've seen "." or ".." so far in this component. We need to continue - // searching. - ++get; - continue; - } - - if (c == '/') { - if (get == component_start || get == component_start + 1) { - // We've found a "/" or a "./", which we can elide. - ++get; - component_start = get; - continue; - } - if (get == component_start + 2) { - // We've found a "../", which means we need to remove the previous - // component. - if (put == traversal_root) { - path[put++] = '.'; - path[put++] = '.'; - path[put++] = '/'; - traversal_root = put; - } else { - put = ResolveParentDirectoryTraversal(path, put); - } - ++get; - component_start = get; - continue; - } - } - - size_t next_separator = path.find('/', get); - if (next_separator == std::string::npos) { - // We've reached the last component. - break; - } - size_t next_component_start = next_separator + 1; - ++next_separator; - size_t component_size = next_component_start - component_start; - if (put != component_start && component_size > 0) { - path.replace(put, component_size, - path.substr(component_start, component_size)); - } - put += component_size; - get = next_component_start; - component_start = next_component_start; - } - - size_t last_component_size = path.size() - component_start; - if (last_component_size == 1 && path[component_start] == '.') { - // The last component is ".", which we can elide. - } else if (last_component_size == 2 && path[component_start] == '.' && - path[component_start + 1] == '.') { - // The last component is "..", which means we need to remove the previous - // component. - if (put == traversal_root) { - path[put++] = '.'; - path[put++] = '.'; - path[put++] = '/'; - traversal_root = put; - } else { - put = ResolveParentDirectoryTraversal(path, put); - } - } else { - // Otherwise, we need to copy over the last component. - if (put != component_start && last_component_size > 0) { - path.replace(put, last_component_size, - path.substr(component_start, last_component_size)); - } - put += last_component_size; - } - - if (put >= 2 && path[put - 1] == '/') - --put; // Trim trailing / - else if (put == 0) - return "."; // Use . for otherwise empty paths to treat them as relative. - - path.resize(put); - return path; -} - -std::string AbsolutePath(const std::string& path) { - if (path.size() > 0) { - if (path[0] == '/') { - // Path is already absolute. - return path; - } - return GetCurrentDirectory() + "/" + path; - } else { - // Path is empty. - return GetCurrentDirectory(); - } -} - -std::string GetDirectoryName(const std::string& path) { - size_t separator = path.rfind('/'); - if (separator == 0u) - return "/"; - if (separator == std::string::npos) - return std::string(); - return path.substr(0, separator); -} - -std::string GetBaseName(const std::string& path) { - size_t separator = path.rfind('/'); - if (separator == std::string::npos) - return path; - return path.substr(separator + 1); -} - -std::string GetAbsoluteFilePath(const std::string& path) { -#if defined(OS_FUCHSIA) - // realpath() isn't supported by Fuchsia. See MG-425. - return SimplifyPath(AbsolutePath(path)); -#else - char buffer[PATH_MAX]; - if (realpath(path.c_str(), buffer) == nullptr) - return std::string(); - return buffer; -#endif // defined(OS_FUCHSIA) -} - -} // namespace filesystem diff --git a/third_party/tonic/filesystem/filesystem/path_win.cc b/third_party/tonic/filesystem/filesystem/path_win.cc deleted file mode 100644 index 48edde610b24e..0000000000000 --- a/third_party/tonic/filesystem/filesystem/path_win.cc +++ /dev/null @@ -1,227 +0,0 @@ -// Copyright 2013 The Flutter 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 "filesystem/path.h" - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -namespace filesystem { -namespace { - -size_t RootLength(const std::string& path) { - if (path.size() == 0) - return 0; - if (path[0] == '/') - return 1; - if (path[0] == '\\') { - if (path.size() < 2 || path[1] != '\\') - return 1; - // The path is a network share. Search for up to two '\'s, as they are - // the server and share - and part of the root part. - size_t index = path.find('\\', 2); - if (index > 0) { - index = path.find('\\', index + 1); - if (index > 0) - return index; - } - return path.size(); - } - // If the path is of the form 'C:/' or 'C:\', with C being any letter, it's - // a root part. - if (path.length() >= 2 && path[1] == ':' && - (path[2] == '/' || path[2] == '\\') && - ((path[0] >= 'A' && path[0] <= 'Z') || - (path[0] >= 'a' && path[0] <= 'z'))) { - return 3; - } - return 0; -} - -size_t IsSeparator(const char sep) { - return sep == '/' || sep == '\\'; -} - -size_t LastSeparator(const std::string& path) { - return path.find_last_of("/\\"); -} - -size_t LastSeparator(const std::string& path, size_t pos) { - return path.find_last_of("/\\", pos); -} - -size_t FirstSeparator(const std::string& path, size_t pos) { - return path.find_first_of("/\\", pos); -} - -size_t ResolveParentDirectoryTraversal(const std::string& path, - size_t put, - size_t root_length) { - if (put <= root_length) { - return root_length; - } - size_t previous_separator = LastSeparator(path, put - 2); - if (previous_separator != std::string::npos) - return previous_separator + 1; - return 0; -} -} // namespace - -std::string SimplifyPath(std::string path) { - if (path.empty()) - return "."; - - size_t put = 0; - size_t get = 0; - size_t traversal_root = 0; - size_t component_start = 0; - - size_t rootLength = RootLength(path); - if (rootLength > 0) { - put = rootLength; - get = rootLength; - component_start = rootLength; - } - - while (get < path.size()) { - char c = path[get]; - - if (c == '.' && (get == component_start || get == component_start + 1)) { - // We've seen "." or ".." so far in this component. We need to continue - // searching. - ++get; - continue; - } - - if (IsSeparator(c)) { - if (get == component_start || get == component_start + 1) { - // We've found a "/" or a "./", which we can elide. - ++get; - component_start = get; - continue; - } - if (get == component_start + 2) { - // We've found a "../", which means we need to remove the previous - // component. - if (put == traversal_root) { - path[put++] = '.'; - path[put++] = '.'; - path[put++] = '\\'; - traversal_root = put; - } else { - put = ResolveParentDirectoryTraversal(path, put, rootLength); - } - ++get; - component_start = get; - continue; - } - } - - size_t next_separator = FirstSeparator(path, get); - if (next_separator == std::string::npos) { - // We've reached the last component. - break; - } - size_t next_component_start = next_separator + 1; - ++next_separator; - size_t component_size = next_component_start - component_start; - if (put != component_start && component_size > 0) { - path.replace(put, component_size, - path.substr(component_start, component_size)); - } - put += component_size; - get = next_component_start; - component_start = next_component_start; - } - - size_t last_component_size = path.size() - component_start; - if (last_component_size == 1 && path[component_start] == '.') { - // The last component is ".", which we can elide. - } else if (last_component_size == 2 && path[component_start] == '.' && - path[component_start + 1] == '.') { - // The last component is "..", which means we need to remove the previous - // component. - if (put == traversal_root) { - path[put++] = '.'; - path[put++] = '.'; - path[put++] = '\\'; - traversal_root = put; - } else { - put = ResolveParentDirectoryTraversal(path, put, rootLength); - } - } else { - // Otherwise, we need to copy over the last component. - if (put != component_start && last_component_size > 0) { - path.replace(put, last_component_size, - path.substr(component_start, last_component_size)); - } - put += last_component_size; - } - - if (put >= 2 && IsSeparator(path[put - 1])) - --put; // Trim trailing / - else if (put == 0) - return "."; // Use . for otherwise empty paths to treat them as relative. - - path.resize(put); - std::replace(path.begin(), path.end(), '/', '\\'); - return path; -} - -std::string AbsolutePath(const std::string& path) { - char absPath[MAX_PATH]; - _fullpath(absPath, path.c_str(), MAX_PATH); - return std::string(absPath); -} - -std::string GetDirectoryName(const std::string& path) { - size_t rootLength = RootLength(path); - size_t separator = LastSeparator(path); - if (separator < rootLength) - separator = rootLength; - if (separator == std::string::npos) - return std::string(); - return path.substr(0, separator); -} - -std::string GetBaseName(const std::string& path) { - size_t separator = LastSeparator(path); - if (separator == std::string::npos) - return path; - return path.substr(separator + 1); -} - -std::string GetAbsoluteFilePath(const std::string& path) { - HANDLE file = - CreateFileA(path.c_str(), FILE_READ_ATTRIBUTES, - FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, - OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); - if (file == INVALID_HANDLE_VALUE) { - return std::string(); - } - char buffer[MAX_PATH]; - DWORD ret = - GetFinalPathNameByHandleA(file, buffer, MAX_PATH, FILE_NAME_NORMALIZED); - if (ret == 0 || ret > MAX_PATH) { - CloseHandle(file); - return std::string(); - } - std::string result(buffer); - result.erase(0, strlen("\\\\?\\")); - CloseHandle(file); - return result; -} - -} // namespace filesystem diff --git a/third_party/tonic/filesystem/filesystem/portable_unistd.h b/third_party/tonic/filesystem/filesystem/portable_unistd.h deleted file mode 100644 index d6d55647e268d..0000000000000 --- a/third_party/tonic/filesystem/filesystem/portable_unistd.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2013 The Flutter 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 FILESYSTEM_PORTABLE_UNISTD_H_ -#define FILESYSTEM_PORTABLE_UNISTD_H_ - -#include "tonic/common/build_config.h" - -#if defined(OS_WIN) -#include -#include -#include - -#define STDERR_FILENO _fileno(stderr) -#define PATH_MAX _MAX_PATH - -#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) -#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG) -#define R_OK 4 - -#define mkdir(path, mode) _mkdir(path) - -#else -#include -#endif - -#endif // FILESYSTEM_PORTABLE_UNISTD_H_ diff --git a/third_party/tonic/filesystem/tests/BUILD.gn b/third_party/tonic/filesystem/tests/BUILD.gn deleted file mode 100644 index 04d933b576751..0000000000000 --- a/third_party/tonic/filesystem/tests/BUILD.gn +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2013 The Flutter Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -executable("files_unittests") { - testonly = true - - sources = [ - "directory_unittest.cc", - "file_descriptor_unittest.cc", - "file_unittest.cc", - "files_unittest_main.cc", - "path_unittest.cc", - "scoped_temp_dir_unittest.cc", - ] - - deps = [ - ":../:filesystem", - ] -} diff --git a/third_party/tonic/filesystem/tests/directory_unittest.cc b/third_party/tonic/filesystem/tests/directory_unittest.cc deleted file mode 100644 index c1840e861d8e2..0000000000000 --- a/third_party/tonic/filesystem/tests/directory_unittest.cc +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2013 The Flutter 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 "filesystem/directory.h" -#include "filesystem/path.h" -#include "filesystem/scoped_temp_dir.h" -#include "gtest/gtest.h" - -namespace filesystem { - -TEST(Directory, CreateDirectory) { - std::string cwd = GetCurrentDirectory(); - - ScopedTempDir dir; - EXPECT_TRUE(IsDirectory(dir.path())); - EXPECT_EQ(0, chdir(dir.path().c_str())); - - EXPECT_TRUE(CreateDirectory("foo/bar")); - EXPECT_TRUE(IsDirectory("foo")); - EXPECT_TRUE(IsDirectory("foo/bar")); - EXPECT_FALSE(IsDirectory("foo/bar/baz")); - - EXPECT_TRUE(CreateDirectory("foo/bar/baz")); - EXPECT_TRUE(IsDirectory("foo/bar/baz")); - - EXPECT_TRUE(CreateDirectory("qux")); - EXPECT_TRUE(IsDirectory("qux")); - - EXPECT_EQ(0, chdir(cwd.c_str())); - - std::string abs_path = dir.path() + "/another/one"; - EXPECT_TRUE(CreateDirectory(abs_path)); - EXPECT_TRUE(IsDirectory(abs_path)); -} - -} // namespace filesystem diff --git a/third_party/tonic/filesystem/tests/file_descriptor_unittest.cc b/third_party/tonic/filesystem/tests/file_descriptor_unittest.cc deleted file mode 100644 index 43addb33a2ac3..0000000000000 --- a/third_party/tonic/filesystem/tests/file_descriptor_unittest.cc +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2013 The Flutter 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 -#include - -#include -#include - -#include "filesystem/scoped_temp_dir.h" -#include "filesystem/unique_fd.h" -#include "gtest/gtest.h" - -namespace filesystem { - -TEST(FileDescriptor, WriteAndRead) { - files::ScopedTempDir temp_dir; - std::string path; - ASSERT_TRUE(temp_dir.NewTempFile(&path)); - - fxl::UniqueFD fd(open(path.c_str(), O_RDWR)); - ASSERT_TRUE(fd.is_valid()); - - std::string string = "one, two, three"; - EXPECT_TRUE(WriteFileDescriptor(fd.get(), string.data(), string.size())); - EXPECT_EQ(0, lseek(fd.get(), 0, SEEK_SET)); - - std::vector buffer; - buffer.resize(1024); - ssize_t read = ReadFileDescriptor(fd.get(), buffer.data(), 1024); - EXPECT_EQ(static_cast(string.size()), read); - EXPECT_EQ(string, buffer.data()); -} - -} // namespace filesystem diff --git a/third_party/tonic/filesystem/tests/file_unittest.cc b/third_party/tonic/filesystem/tests/file_unittest.cc deleted file mode 100644 index d1105e39f64a9..0000000000000 --- a/third_party/tonic/filesystem/tests/file_unittest.cc +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2013 The Flutter 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 "filesystem/file.h" - -#include - -#include "filesystem/path.h" -#include "filesystem/scoped_temp_dir.h" -#include "gtest/gtest.h" - -namespace filesystem { -namespace { - -TEST(File, GetFileSize) { - ScopedTempDir dir; - std::string path; - - ASSERT_TRUE(dir.NewTempFile(&path)); - - uint64_t size; - EXPECT_TRUE(GetFileSize(path, &size)); - EXPECT_EQ(0u, size); - - std::string content = "Hello World"; - ASSERT_TRUE(WriteFile(path, content.data(), content.size())); - EXPECT_TRUE(GetFileSize(path, &size)); - EXPECT_EQ(content.size(), size); -} - -TEST(File, WriteFileInTwoPhases) { - ScopedTempDir dir; - std::string path = dir.path() + "/destination"; - - std::string content = "Hello World"; - ASSERT_TRUE(WriteFileInTwoPhases(path, content, dir.path())); - std::string read_content; - ASSERT_TRUE(ReadFileToString(path, &read_content)); - EXPECT_EQ(read_content, content); -} - -#if defined(OS_LINUX) || defined(OS_FUCHSIA) -TEST(File, IsFileAt) { - ScopedTempDir dir; - std::string path; - - ASSERT_TRUE(dir.NewTempFile(&path)); - - fxl::UniqueFD dirfd(open(dir.path().c_str(), O_RDONLY)); - ASSERT_TRUE(dirfd.get() != -1); - EXPECT_TRUE(IsFileAt(dirfd.get(), GetBaseName(path))); -} -#endif - -} // namespace -} // namespace filesystem diff --git a/third_party/tonic/filesystem/tests/files_unittest_main.cc b/third_party/tonic/filesystem/tests/files_unittest_main.cc deleted file mode 100644 index 2b409d838d21d..0000000000000 --- a/third_party/tonic/filesystem/tests/files_unittest_main.cc +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright 2013 The Flutter 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 "gtest/gtest.h" diff --git a/third_party/tonic/filesystem/tests/path_unittest.cc b/third_party/tonic/filesystem/tests/path_unittest.cc deleted file mode 100644 index cc0ccc1edc0f1..0000000000000 --- a/third_party/tonic/filesystem/tests/path_unittest.cc +++ /dev/null @@ -1,234 +0,0 @@ -// Copyright 2013 The Flutter 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 "filesystem/path.h" -#include "filesystem/directory.h" -#include "filesystem/scoped_temp_dir.h" -#include "gtest/gtest.h" -#include "tonic/common/build_config.h" - -namespace filesystem { - -void ExpectPlatformPath(std::string expected, std::string actual) { -#if defined(OS_WIN) - std::replace(expected.begin(), expected.end(), '/', '\\'); -#endif - EXPECT_EQ(expected, actual); -} - -TEST(Path, SimplifyPath) { - ExpectPlatformPath(".", SimplifyPath("")); - ExpectPlatformPath(".", SimplifyPath(".")); - ExpectPlatformPath("..", SimplifyPath("..")); - ExpectPlatformPath("...", SimplifyPath("...")); - - ExpectPlatformPath("/", SimplifyPath("/")); - ExpectPlatformPath("/", SimplifyPath("/.")); - ExpectPlatformPath("/", SimplifyPath("/..")); - ExpectPlatformPath("/...", SimplifyPath("/...")); - - ExpectPlatformPath("foo", SimplifyPath("foo")); - ExpectPlatformPath("foo", SimplifyPath("foo/")); - ExpectPlatformPath("foo", SimplifyPath("foo/.")); - ExpectPlatformPath("foo", SimplifyPath("foo/./")); - ExpectPlatformPath(".", SimplifyPath("foo/..")); - ExpectPlatformPath(".", SimplifyPath("foo/../")); - ExpectPlatformPath("foo/...", SimplifyPath("foo/...")); - ExpectPlatformPath("foo/...", SimplifyPath("foo/.../")); - ExpectPlatformPath("foo/.b", SimplifyPath("foo/.b")); - ExpectPlatformPath("foo/.b", SimplifyPath("foo/.b/")); - - ExpectPlatformPath("/foo", SimplifyPath("/foo")); - ExpectPlatformPath("/foo", SimplifyPath("/foo/")); - ExpectPlatformPath("/foo", SimplifyPath("/foo/.")); - ExpectPlatformPath("/foo", SimplifyPath("/foo/./")); - ExpectPlatformPath("/", SimplifyPath("/foo/..")); - ExpectPlatformPath("/", SimplifyPath("/foo/../")); - ExpectPlatformPath("/foo/...", SimplifyPath("/foo/...")); - ExpectPlatformPath("/foo/...", SimplifyPath("/foo/.../")); - ExpectPlatformPath("/foo/.b", SimplifyPath("/foo/.b")); - ExpectPlatformPath("/foo/.b", SimplifyPath("/foo/.b/")); - - ExpectPlatformPath("foo/bar", SimplifyPath("foo/bar")); - ExpectPlatformPath("foo/bar", SimplifyPath("foo/bar/")); - ExpectPlatformPath("foo/bar", SimplifyPath("foo/./bar")); - ExpectPlatformPath("foo/bar", SimplifyPath("foo/./bar/")); - ExpectPlatformPath("bar", SimplifyPath("foo/../bar")); - ExpectPlatformPath("bar", SimplifyPath("foo/baz/../../bar")); - ExpectPlatformPath("bar", SimplifyPath("foo/../bar/")); - ExpectPlatformPath("foo/.../bar", SimplifyPath("foo/.../bar")); - ExpectPlatformPath("foo/.../bar", SimplifyPath("foo/.../bar/")); - ExpectPlatformPath("foo/.b/bar", SimplifyPath("foo/.b/bar")); - ExpectPlatformPath("foo/.b/bar", SimplifyPath("foo/.b/bar/")); - - ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/bar")); - ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/bar/")); - ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/./bar")); - ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/./bar/")); - ExpectPlatformPath("/bar", SimplifyPath("/foo/../bar")); - ExpectPlatformPath("/bar", SimplifyPath("/foo/../bar/")); - ExpectPlatformPath("/foo/.../bar", SimplifyPath("/foo/.../bar")); - ExpectPlatformPath("/foo/.../bar", SimplifyPath("/foo/.../bar/")); - ExpectPlatformPath("/foo/.b/bar", SimplifyPath("/foo/.b/bar")); - ExpectPlatformPath("/foo/.b/bar", SimplifyPath("/foo/.b/bar/")); - - ExpectPlatformPath("../foo", SimplifyPath("../foo")); - ExpectPlatformPath("../../bar", SimplifyPath("../foo/../../bar")); - ExpectPlatformPath("/bar", SimplifyPath("/foo/../../bar")); - - // Already clean - ExpectPlatformPath(".", SimplifyPath("")); - ExpectPlatformPath("abc", SimplifyPath("abc")); - ExpectPlatformPath("abc/def", SimplifyPath("abc/def")); - ExpectPlatformPath("a/b/c", SimplifyPath("a/b/c")); - ExpectPlatformPath(".", SimplifyPath(".")); - ExpectPlatformPath("..", SimplifyPath("..")); - ExpectPlatformPath("../..", SimplifyPath("../..")); - ExpectPlatformPath("../../abc", SimplifyPath("../../abc")); - ExpectPlatformPath("/abc", SimplifyPath("/abc")); - ExpectPlatformPath("/", SimplifyPath("/")); - - // Remove trailing slash - ExpectPlatformPath("abc", SimplifyPath("abc/")); - ExpectPlatformPath("abc/def", SimplifyPath("abc/def/")); - ExpectPlatformPath("a/b/c", SimplifyPath("a/b/c/")); - ExpectPlatformPath(".", SimplifyPath("./")); - ExpectPlatformPath("..", SimplifyPath("../")); - ExpectPlatformPath("../..", SimplifyPath("../../")); - ExpectPlatformPath("/abc", SimplifyPath("/abc/")); - - // Remove doubled slash - ExpectPlatformPath("abc/def/ghi", SimplifyPath("abc//def//ghi")); - ExpectPlatformPath("/abc", SimplifyPath("//abc")); - ExpectPlatformPath("/abc", SimplifyPath("///abc")); - ExpectPlatformPath("/abc", SimplifyPath("//abc//")); - ExpectPlatformPath("abc", SimplifyPath("abc//")); - - // Remove . elements - ExpectPlatformPath("abc/def", SimplifyPath("abc/./def")); - ExpectPlatformPath("/abc/def", SimplifyPath("/./abc/def")); - ExpectPlatformPath("abc", SimplifyPath("abc/.")); - - // Remove .. elements - ExpectPlatformPath("abc/def/jkl", SimplifyPath("abc/def/ghi/../jkl")); - ExpectPlatformPath("abc/jkl", SimplifyPath("abc/def/../ghi/../jkl")); - ExpectPlatformPath("abc", SimplifyPath("abc/def/..")); - ExpectPlatformPath(".", SimplifyPath("abc/def/../..")); - ExpectPlatformPath("/", SimplifyPath("/abc/def/../..")); - ExpectPlatformPath("..", SimplifyPath("abc/def/../../..")); - ExpectPlatformPath("/", SimplifyPath("/abc/def/../../..")); - ExpectPlatformPath("../../mno", - SimplifyPath("abc/def/../../../ghi/jkl/../../../mno")); - ExpectPlatformPath("/mno", SimplifyPath("/../mno")); - - // Combinations - ExpectPlatformPath("def", SimplifyPath("abc/./../def")); - ExpectPlatformPath("def", SimplifyPath("abc//./../def")); - ExpectPlatformPath("../../def", SimplifyPath("abc/../../././../def")); - -#if defined(OS_WIN) - ExpectPlatformPath("a\\c", SimplifyPath("a\\b\\..\\c")); - ExpectPlatformPath("X:\\a\\c", SimplifyPath("X:/a/b/../c")); - ExpectPlatformPath("X:\\a\\b\\c", SimplifyPath("X:/a/b/./c")); - ExpectPlatformPath("X:\\c", SimplifyPath("X:/../../c")); -#endif -} - -TEST(Path, AbsolutePath) { -#if defined(OS_WIN) - // We cut out the drive letter as it can be different on every system. - EXPECT_EQ(":\\foo\\bar", AbsolutePath("\\foo\\bar").substr(1)); - EXPECT_EQ(":\\foo\\bar", AbsolutePath("/foo/bar").substr(1)); - EXPECT_EQ(":\\foo\\bar\\", AbsolutePath("\\foo\\bar\\").substr(1)); - EXPECT_EQ(":\\foo\\bar\\", AbsolutePath("/foo/bar/").substr(1)); - EXPECT_EQ("C:\\foo\\bar\\", AbsolutePath("C:\\foo\\bar\\")); - EXPECT_EQ(GetCurrentDirectory() + "\\foo", AbsolutePath("foo")); -#else - EXPECT_EQ("/foo/bar", AbsolutePath("/foo/bar")); - EXPECT_EQ("/foo/bar/", AbsolutePath("/foo/bar/")); - EXPECT_EQ(GetCurrentDirectory() + "/foo", AbsolutePath("foo")); -#endif - EXPECT_EQ(GetCurrentDirectory(), AbsolutePath("")); -} - -TEST(Path, GetDirectoryName) { - EXPECT_EQ("foo", GetDirectoryName("foo/")); - EXPECT_EQ("foo/bar", GetDirectoryName("foo/bar/")); - EXPECT_EQ("foo", GetDirectoryName("foo/bar")); - EXPECT_EQ("foo/bar", GetDirectoryName("foo/bar/..")); - EXPECT_EQ("foo/bar/..", GetDirectoryName("foo/bar/../..")); - EXPECT_EQ("", GetDirectoryName("foo")); - EXPECT_EQ("/", GetDirectoryName("/")); - EXPECT_EQ("", GetDirectoryName("a")); - EXPECT_EQ("/", GetDirectoryName("/a")); - EXPECT_EQ("/a", GetDirectoryName("/a/")); - EXPECT_EQ("a", GetDirectoryName("a/")); -#if defined(OS_WIN) - EXPECT_EQ("C:\\", GetDirectoryName("C:\\")); - EXPECT_EQ("C:\\foo", GetDirectoryName("C:\\foo\\")); - EXPECT_EQ("C:\\foo", GetDirectoryName("C:\\foo\\bar")); - EXPECT_EQ("foo\\bar", GetDirectoryName("foo\\bar\\")); - EXPECT_EQ("foo", GetDirectoryName("foo\\bar")); - EXPECT_EQ("\\", GetDirectoryName("\\")); - EXPECT_EQ("\\", GetDirectoryName("\\a")); -#endif -} - -TEST(Path, GetBaseName) { - EXPECT_EQ("", GetBaseName("foo/")); - EXPECT_EQ("", GetBaseName("foo/bar/")); - EXPECT_EQ("bar", GetBaseName("foo/bar")); - EXPECT_EQ("..", GetBaseName("foo/bar/..")); - EXPECT_EQ("..", GetBaseName("foo/bar/../..")); - EXPECT_EQ("foo", GetBaseName("foo")); - EXPECT_EQ("", GetBaseName("/")); - EXPECT_EQ("a", GetBaseName("a")); - EXPECT_EQ("a", GetBaseName("/a")); - EXPECT_EQ("", GetBaseName("/a/")); - EXPECT_EQ("", GetBaseName("a/")); -#if defined(OS_WIN) - EXPECT_EQ("", GetBaseName("C:\\")); - EXPECT_EQ("", GetBaseName("C:\\foo\\")); - EXPECT_EQ("bar", GetBaseName("C:\\foo\\bar")); - EXPECT_EQ("", GetBaseName("foo\\bar\\")); - EXPECT_EQ("bar", GetBaseName("foo\\bar")); - EXPECT_EQ("", GetBaseName("\\")); - EXPECT_EQ("a", GetBaseName("\\a")); -#endif -} - -TEST(Path, DeletePath) { - ScopedTempDir dir; - - std::string sub_dir = dir.path() + "/dir"; - CreateDirectory(sub_dir); - EXPECT_TRUE(IsDirectory(sub_dir)); - EXPECT_TRUE(DeletePath(sub_dir, false)); - EXPECT_FALSE(IsDirectory(sub_dir)); -} - -TEST(Path, DeletePathRecursively) { - ScopedTempDir dir; - - std::string sub_dir = dir.path() + "/dir"; - CreateDirectory(sub_dir); - EXPECT_TRUE(IsDirectory(sub_dir)); - - std::string sub_sub_dir1 = sub_dir + "/dir1"; - CreateDirectory(sub_sub_dir1); - EXPECT_TRUE(IsDirectory(sub_sub_dir1)); - std::string sub_sub_dir2 = sub_dir + "/dir2"; - CreateDirectory(sub_sub_dir2); - EXPECT_TRUE(IsDirectory(sub_sub_dir2)); - - EXPECT_FALSE(DeletePath(sub_dir, false)); - EXPECT_TRUE(IsDirectory(sub_dir)); - EXPECT_TRUE(IsDirectory(sub_sub_dir1)); - - EXPECT_TRUE(DeletePath(sub_dir, true)); - EXPECT_FALSE(IsDirectory(sub_dir)); - EXPECT_FALSE(IsDirectory(sub_sub_dir1)); -} - -} // namespace filesystem diff --git a/third_party/tonic/filesystem/tests/scoped_temp_dir_unittest.cc b/third_party/tonic/filesystem/tests/scoped_temp_dir_unittest.cc deleted file mode 100644 index 659051df6966e..0000000000000 --- a/third_party/tonic/filesystem/tests/scoped_temp_dir_unittest.cc +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2013 The Flutter 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 "filesystem/scoped_temp_dir.h" -#include "filesystem/directory.h" -#include "filesystem/path.h" -#include "gtest/gtest.h" - -namespace filesystem { - -TEST(ScopedTempDir, Creation) { - ScopedTempDir dir; - - EXPECT_TRUE(IsDirectory(dir.path())); -} - -TEST(ScopedTempDir, Deletion) { - std::string path; - { - ScopedTempDir dir; - path = dir.path(); - } - - EXPECT_FALSE(IsDirectory(path)); -} - -TEST(ScopedTempDir, NewTempFile) { - ScopedTempDir dir; - std::string path; - EXPECT_TRUE(dir.NewTempFile(&path)); - EXPECT_FALSE(path.empty()); -} - -TEST(ScopedTempDir, CustomParent) { - ScopedTempDir root_dir; - std::string parent = root_dir.path() + "/a/b/c"; - std::string path; - { - ScopedTempDir dir(parent); - path = dir.path(); - EXPECT_TRUE(IsDirectory(path)); - EXPECT_EQ(path.substr(0, parent.size()), parent); - EXPECT_NE("temp_dir_XXXXXX", GetBaseName(path)); - - // Regression test - don't create temp_dir_XXXXXX dir next to the temp one. - EXPECT_FALSE( - files::IsDirectory(GetDirectoryName(path) + "/temp_dir_XXXXXX")); - } - - // Verify that the tmp directory itself was deleted, but not the parent. - EXPECT_FALSE(IsDirectory(path)); - EXPECT_TRUE(IsDirectory(parent)); -} - -} // namespace filesystem