From 66832444565bd5a5a83b64ffc305cd510e4536c5 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Thu, 5 Dec 2024 11:17:25 -0800 Subject: [PATCH] Introduce `shadow_copy` parameter to `PathToResourceAsFile` PiperOrigin-RevId: 703182436 --- mediapipe/util/BUILD | 1 - mediapipe/util/resource_util.h | 12 ++++++------ mediapipe/util/resource_util_android.cc | 17 ++++++++++++----- mediapipe/util/resource_util_apple.cc | 5 +++-- mediapipe/util/resource_util_default.cc | 5 +++-- mediapipe/util/resource_util_emscripten.cc | 6 ++++-- mediapipe/util/resource_util_windows.cc | 5 +++-- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/mediapipe/util/BUILD b/mediapipe/util/BUILD index 2d8f851857..d8cccf0b46 100644 --- a/mediapipe/util/BUILD +++ b/mediapipe/util/BUILD @@ -281,7 +281,6 @@ cc_library( "//mediapipe/framework/port:ret_check", "//mediapipe/framework/port:singleton", "//mediapipe/framework/port:status", - "//mediapipe/framework/port:statusor", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/log:absl_log", "@com_google_absl//absl/status", diff --git a/mediapipe/util/resource_util.h b/mediapipe/util/resource_util.h index 40f6839b84..c8a0e93914 100644 --- a/mediapipe/util/resource_util.h +++ b/mediapipe/util/resource_util.h @@ -28,9 +28,9 @@ namespace mediapipe { // - If the input path is an absolute path, it is returned as-is. // - If the input path is relative, it is searched in a platform-specific // location: -// - On Android, we look for an asset with the given relative path; if -// it exists, it is copied to the file system (using the AssetCache), -// and a path to that file is returned. +// - On Android with `shadow_copy`, we look for an asset with the given +// relative path; if it exists, it is copied to the file system (using +// the AssetCache), and a path to that file is returned. // - On iOS, we look for a resource with the given relative path in the // application bundle. // @@ -38,9 +38,9 @@ namespace mediapipe { // Note: This function should be used by code that needs a resource to be // accessible as a normal file, usually to call an existing API that only // accepts file paths. Code that can access data as a stream or as a buffer -// should read from an asset directly on Android; an API for this will be -// provided later. TODO. -absl::StatusOr PathToResourceAsFile(const std::string& path); +// should use the Resources API (see below). +absl::StatusOr PathToResourceAsFile(const std::string& path, + bool shadow_copy = true); // DEPRECATED: use `CalculatorContext::GetResources` and // `SubgraphContext::GetResources` which allow for fine grained per graph diff --git a/mediapipe/util/resource_util_android.cc b/mediapipe/util/resource_util_android.cc index 8678b97312..431b9dca3d 100644 --- a/mediapipe/util/resource_util_android.cc +++ b/mediapipe/util/resource_util_android.cc @@ -15,11 +15,13 @@ #include #include "absl/log/absl_log.h" +#include "absl/status/statusor.h" #include "absl/strings/match.h" +#include "absl/strings/str_cat.h" #include "mediapipe/framework/port/file_helpers.h" #include "mediapipe/framework/port/ret_check.h" #include "mediapipe/framework/port/singleton.h" -#include "mediapipe/framework/port/statusor.h" +#include "mediapipe/framework/port/status_builder.h" #include "mediapipe/util/android/asset_manager_util.h" #include "mediapipe/util/android/file/base/helpers.h" @@ -27,7 +29,11 @@ namespace mediapipe { namespace { absl::StatusOr PathToResourceAsFileInternal( - const std::string& path) { + const std::string& path, bool shadow_copy) { + if (!shadow_copy) { + return absl::UnavailableError(absl::StrCat( + "Not copying asset '", path, "' due to `shadow_copy == false`")); + } return Singleton::get()->CachedFileFromAsset(path); } } // namespace @@ -65,7 +71,8 @@ absl::Status DefaultGetResourceContents(const std::string& path, } } // namespace internal -absl::StatusOr PathToResourceAsFile(const std::string& path) { +absl::StatusOr PathToResourceAsFile(const std::string& path, + bool shadow_copy) { // Return full path. if (absl::StartsWith(path, "/")) { return path; @@ -73,7 +80,7 @@ absl::StatusOr PathToResourceAsFile(const std::string& path) { // Try to load a relative path or a base filename as is. { - auto status_or_path = PathToResourceAsFileInternal(path); + auto status_or_path = PathToResourceAsFileInternal(path, shadow_copy); if (status_or_path.ok()) { ABSL_LOG(INFO) << "Successfully loaded: " << path; return status_or_path; @@ -86,7 +93,7 @@ absl::StatusOr PathToResourceAsFile(const std::string& path) { RET_CHECK(last_slash_idx != std::string::npos) << path << " doesn't have a slash in it"; // Make sure it's a path. auto base_name = path.substr(last_slash_idx + 1); - auto status_or_path = PathToResourceAsFileInternal(base_name); + auto status_or_path = PathToResourceAsFileInternal(base_name, shadow_copy); if (status_or_path.ok()) { ABSL_LOG(INFO) << "Successfully loaded: " << base_name; return status_or_path; diff --git a/mediapipe/util/resource_util_apple.cc b/mediapipe/util/resource_util_apple.cc index b78be35824..d2ba375b4b 100644 --- a/mediapipe/util/resource_util_apple.cc +++ b/mediapipe/util/resource_util_apple.cc @@ -18,10 +18,10 @@ #include #include "absl/log/absl_log.h" +#include "absl/status/statusor.h" #include "absl/strings/match.h" #include "mediapipe/framework/port/file_helpers.h" #include "mediapipe/framework/port/ret_check.h" -#include "mediapipe/framework/port/statusor.h" #include "mediapipe/util/resource_util.h" namespace mediapipe { @@ -55,7 +55,8 @@ absl::Status DefaultGetResourceContents(const std::string& path, } } // namespace internal -absl::StatusOr PathToResourceAsFile(const std::string& path) { +absl::StatusOr PathToResourceAsFile(const std::string& path, + bool /*shadow_copy*/) { // Return full path. if (absl::StartsWith(path, "/")) { return path; diff --git a/mediapipe/util/resource_util_default.cc b/mediapipe/util/resource_util_default.cc index 3ebbd1f34b..9197295481 100644 --- a/mediapipe/util/resource_util_default.cc +++ b/mediapipe/util/resource_util_default.cc @@ -15,9 +15,9 @@ #include #include "absl/flags/flag.h" +#include "absl/status/statusor.h" #include "mediapipe/framework/deps/file_path.h" #include "mediapipe/framework/port/file_helpers.h" -#include "mediapipe/framework/port/statusor.h" ABSL_FLAG( std::string, resource_root_dir, "", @@ -38,7 +38,8 @@ absl::Status DefaultGetResourceContents(const std::string& path, } } // namespace internal -absl::StatusOr PathToResourceAsFile(const std::string& path) { +absl::StatusOr PathToResourceAsFile(const std::string& path, + bool /*shadow_copy*/) { if (absl::StartsWith(path, "/")) { return path; } diff --git a/mediapipe/util/resource_util_emscripten.cc b/mediapipe/util/resource_util_emscripten.cc index 1243ad115f..ca3d386dc4 100644 --- a/mediapipe/util/resource_util_emscripten.cc +++ b/mediapipe/util/resource_util_emscripten.cc @@ -16,14 +16,16 @@ #include #include "absl/log/absl_log.h" +#include "absl/status/statusor.h" #include "absl/strings/str_format.h" #include "mediapipe/framework/port/file_helpers.h" #include "mediapipe/framework/port/ret_check.h" -#include "mediapipe/framework/port/statusor.h" +#include "mediapipe/util/resource_util.h" namespace mediapipe { -absl::StatusOr PathToResourceAsFile(const std::string& path) { +absl::StatusOr PathToResourceAsFile(const std::string& path, + bool /*shadow_copy*/) { if (absl::StartsWith(path, "/")) { return path; } diff --git a/mediapipe/util/resource_util_windows.cc b/mediapipe/util/resource_util_windows.cc index 6d620e58c4..e28094eef3 100644 --- a/mediapipe/util/resource_util_windows.cc +++ b/mediapipe/util/resource_util_windows.cc @@ -15,10 +15,10 @@ #include #include "absl/flags/flag.h" +#include "absl/status/statusor.h" #include "mediapipe/framework/deps/file_path.h" #include "mediapipe/framework/port/file_helpers.h" #include "mediapipe/framework/port/singleton.h" -#include "mediapipe/framework/port/statusor.h" #include "tools/cpp/runfiles/runfiles.h" ABSL_FLAG( @@ -70,7 +70,8 @@ absl::Status DefaultGetResourceContents(const std::string& path, } // namespace internal -absl::StatusOr PathToResourceAsFile(const std::string& path) { +absl::StatusOr PathToResourceAsFile(const std::string& path, + bool /*shadow_copy*/) { std::string qualified_path = path; if (absl::StartsWith(qualified_path, "./")) { qualified_path = "mediapipe" + qualified_path.substr(1);