Skip to content

Commit

Permalink
Migrate away from status builders
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 703253302
  • Loading branch information
MediaPipe Team authored and copybara-github committed Dec 5, 2024
1 parent f4f40e5 commit 29e87ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
35 changes: 14 additions & 21 deletions mediapipe/framework/deps/file_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include "mediapipe/framework/deps/file_helpers.h"

#include "absl/strings/str_cat.h"

#ifdef _WIN32
#include <Windows.h>
#include <direct.h>
Expand All @@ -30,12 +28,10 @@
#include <string>

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/deps/platform_strings.h" // IWYU pragma: keep
#include "mediapipe/framework/port/canonical_errors.h"
#include "mediapipe/framework/port/status.h"
#include "mediapipe/framework/port/status_builder.h"
#include "mediapipe/framework/port/status_macros.h"

namespace mediapipe {
Expand Down Expand Up @@ -151,17 +147,16 @@ absl::Status GetContents(absl::string_view path, std::string* output,
bool read_as_binary) {
FILE* fp = fopen(std::string(path).c_str(), read_as_binary ? "rb" : "r");
if (fp == NULL) {
return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC)
<< "Can't find file: " << path;
return absl::InvalidArgumentError(absl::StrCat("Can't find file: ", path));
}

output->clear();
while (!feof(fp)) {
char buf[4096];
size_t ret = fread(buf, 1, 4096, fp);
if (ret == 0 && ferror(fp)) {
return mediapipe::UnavailableErrorBuilder(MEDIAPIPE_LOC)
<< "Error while reading file: " << path;
return absl::UnavailableError(
absl::StrCat("Error while reading file: ", path));
}
output->append(std::string(buf, ret));
}
Expand All @@ -172,16 +167,15 @@ absl::Status GetContents(absl::string_view path, std::string* output,
absl::Status SetContents(absl::string_view path, absl::string_view content) {
FILE* fp = fopen(std::string(path).c_str(), "wb");
if (fp == NULL) {
return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC)
<< "Can't open file: " << path;
return absl::InvalidArgumentError(absl::StrCat("Can't open file: ", path));
}

fwrite(content.data(), sizeof(char), content.size(), fp);
size_t write_error = ferror(fp);
if (fclose(fp) != 0 || write_error) {
return mediapipe::UnavailableErrorBuilder(MEDIAPIPE_LOC)
<< "Error while writing file: " << path
<< ". Error message: " << strerror(write_error);
return absl::UnavailableError(
absl::StrCat("Error while writing file: ", path,
". Error message: ", strerror(write_error)));
}
return absl::OkStatus();
}
Expand All @@ -190,16 +184,15 @@ absl::Status AppendStringToFile(absl::string_view path,
absl::string_view contents) {
FILE* fp = fopen(std::string(path).c_str(), "ab");
if (!fp) {
return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC)
<< "Can't open file: " << path;
return absl::InvalidArgumentError(absl::StrCat("Can't open file: ", path));
}

fwrite(contents.data(), sizeof(char), contents.size(), fp);
size_t write_error = ferror(fp);
if (fclose(fp) != 0 || write_error) {
return mediapipe::UnavailableErrorBuilder(MEDIAPIPE_LOC)
<< "Error while writing file: " << path
<< ". Error message: " << strerror(write_error);
return absl::UnavailableError(
absl::StrCat("Error while writing file: ", path,
". Error message: ", strerror(write_error)));
}
return absl::OkStatus();
}
Expand Down Expand Up @@ -252,7 +245,7 @@ absl::Status Exists(absl::string_view file_name) {
}
switch (errno) {
case EACCES:
return mediapipe::PermissionDeniedError("Insufficient permissions.");
return absl::PermissionDeniedError("Insufficient permissions.");
default:
return absl::NotFoundError(
absl::StrCat("The path does not exist: ", file_name));
Expand Down Expand Up @@ -294,7 +287,7 @@ absl::Status RecursivelyCreateDir(absl::string_view path) {
if (mkdir(std::string(path)) != 0) {
switch (errno) {
case EACCES:
return mediapipe::PermissionDeniedError("Insufficient permissions.");
return absl::PermissionDeniedError("Insufficient permissions.");
default:
return absl::UnavailableError("Failed to create directory.");
}
Expand Down
8 changes: 4 additions & 4 deletions mediapipe/framework/deps/file_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MEDIAPIPE_DEPS_FILE_HELPERS_H_
#define MEDIAPIPE_DEPS_FILE_HELPERS_H_
#ifndef MEDIAPIPE_FRAMEWORK_DEPS_FILE_HELPERS_H_
#define MEDIAPIPE_FRAMEWORK_DEPS_FILE_HELPERS_H_

#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "mediapipe/framework/port/status.h"

namespace mediapipe {
namespace file {
Expand Down Expand Up @@ -46,4 +46,4 @@ absl::Status RecursivelyCreateDir(absl::string_view path);
} // namespace file
} // namespace mediapipe

#endif // MEDIAPIPE_DEPS_FILE_HELPERS_H_
#endif // MEDIAPIPE_FRAMEWORK_DEPS_FILE_HELPERS_H_

0 comments on commit 29e87ad

Please sign in to comment.