Skip to content

Commit

Permalink
Add mediapipe::file::IsDirectory helper
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 596637172
  • Loading branch information
MediaPipe Team authored and copybara-github committed Jan 8, 2024
1 parent bdcf474 commit 0b53972
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions mediapipe/framework/deps/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ cc_library(
deps = [
":file_path",
"//mediapipe/framework/port:status",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
)
Expand Down
20 changes: 20 additions & 0 deletions mediapipe/framework/deps/file_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#include <sys/stat.h>

#include <cerrno>
#include <string>

#include "absl/status/status.h"
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/port/canonical_errors.h"
#include "mediapipe/framework/port/status.h"
Expand Down Expand Up @@ -251,6 +253,24 @@ absl::Status Exists(absl::string_view file_name) {
}
}

absl::Status IsDirectory(absl::string_view file_name) {
struct stat buffer;
int status;
status = stat(std::string(file_name).c_str(), &buffer);
if (status == 0) {
if ((buffer.st_mode & S_IFMT) == S_IFDIR) {
return absl::OkStatus();
}
return absl::FailedPreconditionError("The path is not a directory.");
}
switch (errno) {
case EACCES:
return absl::PermissionDeniedError("Insufficient permissions.");
default:
return absl::NotFoundError("The path does not exist.");
}
}

#ifndef _WIN32
int mkdir(std::string path) {
return ::mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
Expand Down
2 changes: 2 additions & 0 deletions mediapipe/framework/deps/file_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ absl::Status MatchFileTypeInDirectory(const std::string& directory,

absl::Status Exists(absl::string_view file_name);

absl::Status IsDirectory(absl::string_view file_name);

absl::Status RecursivelyCreateDir(absl::string_view path);

} // namespace file
Expand Down

0 comments on commit 0b53972

Please sign in to comment.