From 42978d3e6939714e30898a8136731a0c4c5ad099 Mon Sep 17 00:00:00 2001 From: Yuqi Li Date: Wed, 5 Oct 2022 20:41:12 -0700 Subject: [PATCH] MediaPipe MedataExtractor: use absl::Cleanup for unzClose / unzCloseCurrentFile. PiperOrigin-RevId: 479211019 --- mediapipe/tasks/cc/metadata/BUILD | 1 + .../tasks/cc/metadata/metadata_extractor.cc | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/mediapipe/tasks/cc/metadata/BUILD b/mediapipe/tasks/cc/metadata/BUILD index c3555e4a..c1945044 100644 --- a/mediapipe/tasks/cc/metadata/BUILD +++ b/mediapipe/tasks/cc/metadata/BUILD @@ -21,6 +21,7 @@ cc_library( "//mediapipe/tasks/cc:common", "//mediapipe/tasks/cc/metadata/utils:zip_readonly_mem_file", "//mediapipe/tasks/metadata:metadata_schema_cc", + "@com_google_absl//absl/cleanup", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/memory", "@com_google_absl//absl/status", diff --git a/mediapipe/tasks/cc/metadata/metadata_extractor.cc b/mediapipe/tasks/cc/metadata/metadata_extractor.cc index fcec4908..31e59ea9 100644 --- a/mediapipe/tasks/cc/metadata/metadata_extractor.cc +++ b/mediapipe/tasks/cc/metadata/metadata_extractor.cc @@ -17,6 +17,7 @@ limitations under the License. #include +#include "absl/cleanup/cleanup.h" #include "absl/memory/memory.h" #include "absl/status/status.h" #include "absl/strings/str_format.h" @@ -79,6 +80,12 @@ absl::StatusOr GetCurrentZipFileInfo(const unzFile& zf) { int method; MP_RETURN_IF_ERROR(UnzipErrorToStatus( unzOpenCurrentFile2(zf, &method, /*level=*/nullptr, /*raw=*/1))); + absl::Cleanup unzipper_closer = [zf]() { + auto status = UnzipErrorToStatus(unzCloseCurrentFile(zf)); + if (!status.ok()) { + LOG(ERROR) << "Failed to close the current zip file: " << status; + } + }; if (method != Z_NO_COMPRESSION) { return CreateStatusWithPayload( StatusCode::kUnknown, "Expected uncompressed zip archive.", @@ -110,6 +117,8 @@ absl::StatusOr GetCurrentZipFileInfo(const unzFile& zf) { MediaPipeTasksStatus::kMetadataAssociatedFileZipError); } + // Perform the cleanup manually for error propagation. + std::move(unzipper_closer).Cancel(); // Close file and return. MP_RETURN_IF_ERROR(UnzipErrorToStatus(unzCloseCurrentFile(zf))); @@ -247,6 +256,11 @@ absl::Status ModelMetadataExtractor::ExtractAssociatedFiles( // model. return absl::OkStatus(); } + absl::Cleanup unzipper_closer = [zf]() { + if (unzClose(zf) != UNZ_OK) { + LOG(ERROR) << "Unable to close zip archive."; + } + }; // Get number of files. unz_global_info global_info; if (unzGetGlobalInfo(zf, &global_info) != UNZ_OK) { @@ -272,6 +286,9 @@ absl::Status ModelMetadataExtractor::ExtractAssociatedFiles( MediaPipeTasksStatus::kMetadataAssociatedFileZipError); } } + + // Perform the cleanup manually for error propagation. + std::move(unzipper_closer).Cancel(); // Close zip. if (unzClose(zf) != UNZ_OK) { return CreateStatusWithPayload(