aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Jonathan Hseu <jhseu@google.com>2016-11-04 11:53:50 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-11-04 13:05:51 -0700
commit879e0accd1c833771c8058d3eb5f2d4f06f895d4 (patch)
tree81635e1bd758cd8a8e5ec760db70fc72f6480c66
parenta2c8e99777cc0b961fb42106af5bd527a43e76f7 (diff)
Change FileExists to return tensorflow::Status.
Also done separately by @llhe at github.com/tensorflow/tensorflow/pull/5370. We needed to do this change internally to fix all callers. Motivation: The existing FileExists interface doesn't allow callers to distinguish between file not found vs. filesystem errors. Semantics changes: - gfile.Exists in Python now throws an exception for filesystem errors. It continues to return true/false if it can accurately determine whether a file exists. - RecursivelyCreateDir now returns errors for filesystem errors when calling FileExists. Change: 138224013
-rw-r--r--RELEASE.md3
-rw-r--r--tensorflow/cc/saved_model/loader.cc10
-rw-r--r--tensorflow/cc/saved_model/loader_test.cc2
-rw-r--r--tensorflow/contrib/android/asset_manager_filesystem.cc7
-rw-r--r--tensorflow/contrib/android/asset_manager_filesystem.h2
-rw-r--r--tensorflow/contrib/session_bundle/session_bundle.cc12
-rw-r--r--tensorflow/core/debug/debug_io_utils.cc8
-rw-r--r--tensorflow/core/debug/debug_io_utils_test.cc6
-rw-r--r--tensorflow/core/kernels/debug_ops_test.cc2
-rw-r--r--tensorflow/core/platform/cloud/gcs_file_system.cc23
-rw-r--r--tensorflow/core/platform/cloud/gcs_file_system.h2
-rw-r--r--tensorflow/core/platform/cloud/gcs_file_system_test.cc17
-rw-r--r--tensorflow/core/platform/cloud/retrying_file_system.cc2
-rw-r--r--tensorflow/core/platform/cloud/retrying_file_system.h2
-rw-r--r--tensorflow/core/platform/cloud/retrying_file_system_test.cc2
-rw-r--r--tensorflow/core/platform/env.cc6
-rw-r--r--tensorflow/core/platform/env.h4
-rw-r--r--tensorflow/core/platform/env_test.cc26
-rw-r--r--tensorflow/core/platform/file_system.cc19
-rw-r--r--tensorflow/core/platform/file_system.h6
-rw-r--r--tensorflow/core/platform/file_system_test.cc7
-rw-r--r--tensorflow/core/platform/hadoop/hadoop_file_system.cc12
-rw-r--r--tensorflow/core/platform/hadoop/hadoop_file_system.h2
-rw-r--r--tensorflow/core/platform/hadoop/hadoop_file_system_test.cc6
-rw-r--r--tensorflow/core/platform/posix/posix_file_system.cc7
-rw-r--r--tensorflow/core/platform/posix/posix_file_system.h2
-rw-r--r--tensorflow/core/platform/windows/windows_file_system.cc9
-rw-r--r--tensorflow/core/platform/windows/windows_file_system.h2
-rw-r--r--tensorflow/core/util/events_writer.cc2
-rw-r--r--tensorflow/core/util/events_writer_test.cc22
-rw-r--r--tensorflow/core/util/memmapped_file_system.cc9
-rw-r--r--tensorflow/core/util/memmapped_file_system.h2
-rw-r--r--tensorflow/core/util/memmapped_file_system_test.cc5
-rw-r--r--tensorflow/core/util/reporter.cc2
-rw-r--r--tensorflow/core/util/tensor_bundle/tensor_bundle_test.cc2
-rw-r--r--tensorflow/python/lib/io/file_io.i22
-rw-r--r--tensorflow/python/lib/io/file_io.py11
-rw-r--r--tensorflow/stream_executor/cuda/cuda_gpu_executor.cc4
-rw-r--r--tensorflow/stream_executor/lib/env.h4
39 files changed, 172 insertions, 121 deletions
diff --git a/RELEASE.md b/RELEASE.md
index af256fae93..62c9d1b1f6 100644
--- a/RELEASE.md
+++ b/RELEASE.md
@@ -5,6 +5,9 @@
* `BusAdjacency` enum replaced with a protocol buffer `DeviceLocality`. PCI bus
indexing now starts from 1 instead of 0, and bus_id==0 is used where previously
BUS_ANY was used.
+* `Env::FileExists` and `FileSystem::FileExists` now return a tensorflow::Status
+ intead of a bool. Any callers to this function can be converted to a bool
+ by adding .ok() to the call.
# Release 0.11.0
diff --git a/tensorflow/cc/saved_model/loader.cc b/tensorflow/cc/saved_model/loader.cc
index c654d56e8a..c0a890cd16 100644
--- a/tensorflow/cc/saved_model/loader.cc
+++ b/tensorflow/cc/saved_model/loader.cc
@@ -42,13 +42,13 @@ constexpr char kLoadAttemptSuccess[] = "success";
Status ReadSavedModel(const string& export_dir, SavedModel* saved_model_proto) {
const string saved_model_pb_path =
io::JoinPath(export_dir, kSavedModelFilenamePb);
- if (Env::Default()->FileExists(saved_model_pb_path)) {
+ if (Env::Default()->FileExists(saved_model_pb_path).ok()) {
return ReadBinaryProto(Env::Default(), saved_model_pb_path,
saved_model_proto);
}
const string saved_model_pbtxt_path =
io::JoinPath(export_dir, kSavedModelFilenamePbTxt);
- if (Env::Default()->FileExists(saved_model_pbtxt_path)) {
+ if (Env::Default()->FileExists(saved_model_pbtxt_path).ok()) {
return ReadTextProto(Env::Default(), saved_model_pbtxt_path,
saved_model_proto);
}
@@ -118,7 +118,7 @@ Status RunRestore(const RunOptions& run_options, const string& export_dir,
// variables are stored in the variables.data-?????-of-????? files.
const string variables_index_path = io::JoinPath(
variables_directory, MetaFilename(kSavedModelVariablesFilename));
- if (!Env::Default()->FileExists(variables_index_path)) {
+ if (!Env::Default()->FileExists(variables_index_path).ok()) {
return errors::NotFound(
"Checkpoint index file not found in SavedModel directory.");
}
@@ -251,8 +251,8 @@ bool MaybeSavedModelDirectory(const string& export_dir) {
io::JoinPath(export_dir, kSavedModelFilenamePb);
const string saved_model_pbtxt_path =
io::JoinPath(export_dir, kSavedModelFilenamePbTxt);
- return Env::Default()->FileExists(saved_model_pb_path) ||
- Env::Default()->FileExists(saved_model_pbtxt_path);
+ return Env::Default()->FileExists(saved_model_pb_path).ok() ||
+ Env::Default()->FileExists(saved_model_pbtxt_path).ok();
}
} // namespace tensorflow
diff --git a/tensorflow/cc/saved_model/loader_test.cc b/tensorflow/cc/saved_model/loader_test.cc
index 55a22e4e81..82f30c23f6 100644
--- a/tensorflow/cc/saved_model/loader_test.cc
+++ b/tensorflow/cc/saved_model/loader_test.cc
@@ -50,7 +50,7 @@ class LoaderTest : public ::testing::Test {
io::JoinPath(export_dir, kSavedModelAssetsDirectory);
const string asset_filename = "foo.txt";
const string asset_filepath = io::JoinPath(asset_directory, asset_filename);
- EXPECT_TRUE(Env::Default()->FileExists(asset_filepath));
+ TF_EXPECT_OK(Env::Default()->FileExists(asset_filepath));
std::vector<Tensor> path_outputs;
TF_ASSERT_OK(
diff --git a/tensorflow/contrib/android/asset_manager_filesystem.cc b/tensorflow/contrib/android/asset_manager_filesystem.cc
index e71351e481..49d179605c 100644
--- a/tensorflow/contrib/android/asset_manager_filesystem.cc
+++ b/tensorflow/contrib/android/asset_manager_filesystem.cc
@@ -115,11 +115,14 @@ AssetManagerFileSystem::AssetManagerFileSystem(AAssetManager* asset_manager,
const string& prefix)
: asset_manager_(asset_manager), prefix_(prefix) {}
-bool AssetManagerFileSystem::FileExists(const string& fname) {
+Status AssetManagerFileSystem::FileExists(const string& fname) {
string path = RemoveAssetPrefix(fname);
auto asset = ScopedAsset(
AAssetManager_open(asset_manager_, path.c_str(), AASSET_MODE_RANDOM));
- return asset.get() != nullptr;
+ if (asset.get() == nullptr) {
+ return errors::NotFound("File ", fname, " not found.");
+ }
+ return Status::OK();
}
Status AssetManagerFileSystem::NewRandomAccessFile(
diff --git a/tensorflow/contrib/android/asset_manager_filesystem.h b/tensorflow/contrib/android/asset_manager_filesystem.h
index b971e82a83..8cc31db9f2 100644
--- a/tensorflow/contrib/android/asset_manager_filesystem.h
+++ b/tensorflow/contrib/android/asset_manager_filesystem.h
@@ -42,7 +42,7 @@ class AssetManagerFileSystem : public FileSystem {
AssetManagerFileSystem(AAssetManager* asset_manager, const string& prefix);
~AssetManagerFileSystem() override = default;
- bool FileExists(const string& fname) override;
+ Status FileExists(const string& fname) override;
Status NewRandomAccessFile(
const string& filename,
std::unique_ptr<RandomAccessFile>* result) override;
diff --git a/tensorflow/contrib/session_bundle/session_bundle.cc b/tensorflow/contrib/session_bundle/session_bundle.cc
index 1055f69448..920a4eca99 100644
--- a/tensorflow/contrib/session_bundle/session_bundle.cc
+++ b/tensorflow/contrib/session_bundle/session_bundle.cc
@@ -107,10 +107,12 @@ string GetVariablesFilename(const StringPiece export_dir) {
const char kVariablesFilename[] = "export";
const string kVariablesIndexFilename = MetaFilename("export"); // V2 ckpts
const char kVariablesFilenamePattern[] = "export-\?\?\?\?\?-of-\?\?\?\?\?";
- if (Env::Default()->FileExists(
- io::JoinPath(export_dir, kVariablesFilename)) ||
- Env::Default()->FileExists(
- io::JoinPath(export_dir, kVariablesIndexFilename))) {
+ if (Env::Default()
+ ->FileExists(io::JoinPath(export_dir, kVariablesFilename))
+ .ok() ||
+ Env::Default()
+ ->FileExists(io::JoinPath(export_dir, kVariablesIndexFilename))
+ .ok()) {
return io::JoinPath(export_dir, kVariablesFilename);
} else {
return io::JoinPath(export_dir, kVariablesFilenamePattern);
@@ -248,7 +250,7 @@ Status LoadSessionBundleFromPathUsingRunOptions(const SessionOptions& options,
bool IsPossibleExportDirectory(const StringPiece directory) {
const string meta_graph_def_path =
io::JoinPath(directory, kMetaGraphDefFilename);
- return Env::Default()->FileExists(meta_graph_def_path);
+ return Env::Default()->FileExists(meta_graph_def_path).ok();
}
} // namespace serving
diff --git a/tensorflow/core/debug/debug_io_utils.cc b/tensorflow/core/debug/debug_io_utils.cc
index 474577a79c..3738dd21d3 100644
--- a/tensorflow/core/debug/debug_io_utils.cc
+++ b/tensorflow/core/debug/debug_io_utils.cc
@@ -175,13 +175,13 @@ Status DebugFileIO::DumpTensorToEventFile(
// static
Status DebugFileIO::RecursiveCreateDir(Env* env, const string& dir) {
- if (env->FileExists(dir) && env->IsDirectory(dir).ok()) {
+ if (env->FileExists(dir).ok() && env->IsDirectory(dir).ok()) {
// The path already exists as a directory. Return OK right away.
return Status::OK();
}
string parent_dir = io::Dirname(dir).ToString();
- if (!env->FileExists(parent_dir)) {
+ if (!env->FileExists(parent_dir).ok()) {
// The parent path does not exist yet, create it first.
Status s = RecursiveCreateDir(env, parent_dir); // Recursive call
if (!s.ok()) {
@@ -189,7 +189,7 @@ Status DebugFileIO::RecursiveCreateDir(Env* env, const string& dir) {
error::FAILED_PRECONDITION,
strings::StrCat("Failed to create directory ", parent_dir));
}
- } else if (env->FileExists(parent_dir) &&
+ } else if (env->FileExists(parent_dir).ok() &&
!env->IsDirectory(parent_dir).ok()) {
// The path exists, but it is a file.
return Status(error::FAILED_PRECONDITION,
@@ -200,7 +200,7 @@ Status DebugFileIO::RecursiveCreateDir(Env* env, const string& dir) {
env->CreateDir(dir);
// Guard against potential race in creating directories by doing a check
// after the CreateDir call.
- if (env->FileExists(dir) && env->IsDirectory(dir).ok()) {
+ if (env->FileExists(dir).ok() && env->IsDirectory(dir).ok()) {
return Status::OK();
} else {
return Status(error::ABORTED,
diff --git a/tensorflow/core/debug/debug_io_utils_test.cc b/tensorflow/core/debug/debug_io_utils_test.cc
index ecdda643c3..37d35c62a0 100644
--- a/tensorflow/core/debug/debug_io_utils_test.cc
+++ b/tensorflow/core/debug/debug_io_utils_test.cc
@@ -170,10 +170,10 @@ TEST_F(DebugIOUtilsTest, DumpTensorToFileCannotCreateDirectory) {
const string test_dir = testing::TmpDir();
const string txt_file_name = strings::StrCat(test_dir, "/baz");
- if (!env_->FileExists(test_dir)) {
+ if (!env_->FileExists(test_dir).ok()) {
ASSERT_TRUE(env_->CreateDir(test_dir).ok());
}
- ASSERT_FALSE(env_->FileExists(txt_file_name));
+ ASSERT_EQ(error::Code::NOT_FOUND, env_->FileExists(txt_file_name).code());
std::unique_ptr<WritableFile> file;
ASSERT_TRUE(env_->NewWritableFile(txt_file_name, &file).ok());
@@ -182,7 +182,7 @@ TEST_F(DebugIOUtilsTest, DumpTensorToFileCannotCreateDirectory) {
file->Close();
// Verify that the path exists and that it is a file, not a directory.
- ASSERT_TRUE(env_->FileExists(txt_file_name));
+ ASSERT_TRUE(env_->FileExists(txt_file_name).ok());
ASSERT_FALSE(env_->IsDirectory(txt_file_name).ok());
// Second, try to dump the tensor to a path that requires "baz" to be a
diff --git a/tensorflow/core/kernels/debug_ops_test.cc b/tensorflow/core/kernels/debug_ops_test.cc
index e526754d31..8b76678c03 100644
--- a/tensorflow/core/kernels/debug_ops_test.cc
+++ b/tensorflow/core/kernels/debug_ops_test.cc
@@ -90,7 +90,7 @@ TEST_F(DebugIdentityOpTest, Int32Success_6_FileURLs) {
test::ExpectTensorEqual<int32>(expected, *GetOutput(0));
for (int i = 0; i < kNumDumpDirs; ++i) {
- ASSERT_TRUE(env_->FileExists(dump_roots[i]));
+ ASSERT_TRUE(env_->FileExists(dump_roots[i]).ok());
ASSERT_TRUE(env_->IsDirectory(dump_roots[i]).ok());
DIR* dir = opendir(dump_roots[i].c_str());
diff --git a/tensorflow/core/platform/cloud/gcs_file_system.cc b/tensorflow/core/platform/cloud/gcs_file_system.cc
index 39228ed869..9b8f191752 100644
--- a/tensorflow/core/platform/cloud/gcs_file_system.cc
+++ b/tensorflow/core/platform/cloud/gcs_file_system.cc
@@ -669,19 +669,26 @@ Status GcsFileSystem::NewReadOnlyMemoryRegionFromFile(
return Status::OK();
}
-bool GcsFileSystem::FileExists(const string& fname) {
+Status GcsFileSystem::FileExists(const string& fname) {
string bucket, object;
- if (!ParseGcsPath(fname, true, &bucket, &object).ok()) {
- LOG(ERROR) << "Could not parse GCS file name " << fname;
- return false;
- }
+ TF_RETURN_IF_ERROR(ParseGcsPath(fname, true, &bucket, &object));
if (object.empty()) {
bool result;
- return BucketExists(bucket, &result).ok() && result;
+ TF_RETURN_IF_ERROR(BucketExists(bucket, &result));
+ if (result) {
+ return Status::OK();
+ }
}
bool result;
- return (ObjectExists(bucket, object, &result).ok() && result) ||
- (FolderExists(fname, &result).ok() && result);
+ TF_RETURN_IF_ERROR(ObjectExists(bucket, object, &result));
+ if (result) {
+ return Status::OK();
+ }
+ TF_RETURN_IF_ERROR(FolderExists(fname, &result));
+ if (result) {
+ return Status::OK();
+ }
+ return errors::NotFound("The specified path ", fname, " was not found.");
}
Status GcsFileSystem::ObjectExists(const string& bucket, const string& object,
diff --git a/tensorflow/core/platform/cloud/gcs_file_system.h b/tensorflow/core/platform/cloud/gcs_file_system.h
index 4a00e9daa4..28e6a85a08 100644
--- a/tensorflow/core/platform/cloud/gcs_file_system.h
+++ b/tensorflow/core/platform/cloud/gcs_file_system.h
@@ -51,7 +51,7 @@ class GcsFileSystem : public FileSystem {
const string& filename,
std::unique_ptr<ReadOnlyMemoryRegion>* result) override;
- bool FileExists(const string& fname) override;
+ Status FileExists(const string& fname) override;
Status Stat(const string& fname, FileStatistics* stat) override;
diff --git a/tensorflow/core/platform/cloud/gcs_file_system_test.cc b/tensorflow/core/platform/cloud/gcs_file_system_test.cc
index 00bf331470..0d336a61a2 100644
--- a/tensorflow/core/platform/cloud/gcs_file_system_test.cc
+++ b/tensorflow/core/platform/cloud/gcs_file_system_test.cc
@@ -496,7 +496,7 @@ TEST(GcsFileSystemTest, FileExists_YesAsObject) {
new FakeHttpRequestFactory(&requests)),
0 /* read ahead bytes */, 5 /* max upload attempts */);
- EXPECT_TRUE(fs.FileExists("gs://bucket/path/file1.txt"));
+ TF_EXPECT_OK(fs.FileExists("gs://bucket/path/file1.txt"));
}
TEST(GcsFileSystemTest, FileExists_YesAsFolder) {
@@ -518,7 +518,7 @@ TEST(GcsFileSystemTest, FileExists_YesAsFolder) {
new FakeHttpRequestFactory(&requests)),
0 /* read ahead bytes */, 5 /* max upload attempts */);
- EXPECT_TRUE(fs.FileExists("gs://bucket/path/subfolder"));
+ TF_EXPECT_OK(fs.FileExists("gs://bucket/path/subfolder"));
}
TEST(GcsFileSystemTest, FileExists_YesAsBucket) {
@@ -536,8 +536,8 @@ TEST(GcsFileSystemTest, FileExists_YesAsBucket) {
new FakeHttpRequestFactory(&requests)),
0 /* read ahead bytes */, 5 /* max upload attempts */);
- EXPECT_TRUE(fs.FileExists("gs://bucket1"));
- EXPECT_TRUE(fs.FileExists("gs://bucket1/"));
+ TF_EXPECT_OK(fs.FileExists("gs://bucket1"));
+ TF_EXPECT_OK(fs.FileExists("gs://bucket1/"));
}
TEST(GcsFileSystemTest, FileExists_NotAsObjectOrFolder) {
@@ -558,7 +558,8 @@ TEST(GcsFileSystemTest, FileExists_NotAsObjectOrFolder) {
new FakeHttpRequestFactory(&requests)),
0 /* read ahead bytes */, 5 /* max upload attempts */);
- EXPECT_FALSE(fs.FileExists("gs://bucket/path/file1.txt"));
+ EXPECT_EQ(errors::Code::NOT_FOUND,
+ fs.FileExists("gs://bucket/path/file1.txt").code());
}
TEST(GcsFileSystemTest, FileExists_NotAsBucket) {
@@ -575,8 +576,10 @@ TEST(GcsFileSystemTest, FileExists_NotAsBucket) {
std::unique_ptr<HttpRequest::Factory>(
new FakeHttpRequestFactory(&requests)),
0 /* read ahead bytes */, 5 /* max upload attempts */);
- EXPECT_FALSE(fs.FileExists("gs://bucket2/"));
- EXPECT_FALSE(fs.FileExists("gs://bucket2"));
+ EXPECT_EQ(errors::Code::INVALID_ARGUMENT,
+ fs.FileExists("gs://bucket2/").code());
+ EXPECT_EQ(errors::Code::INVALID_ARGUMENT,
+ fs.FileExists("gs://bucket2").code());
}
TEST(GcsFileSystemTest, GetChildren_NoItems) {
diff --git a/tensorflow/core/platform/cloud/retrying_file_system.cc b/tensorflow/core/platform/cloud/retrying_file_system.cc
index ccfbc05b6d..7e98cafa8b 100644
--- a/tensorflow/core/platform/cloud/retrying_file_system.cc
+++ b/tensorflow/core/platform/cloud/retrying_file_system.cc
@@ -162,7 +162,7 @@ Status RetryingFileSystem::NewReadOnlyMemoryRegionFromFile(
initial_delay_microseconds_);
}
-bool RetryingFileSystem::FileExists(const string& fname) {
+Status RetryingFileSystem::FileExists(const string& fname) {
// No status -- no retries.
return base_file_system_->FileExists(fname);
}
diff --git a/tensorflow/core/platform/cloud/retrying_file_system.h b/tensorflow/core/platform/cloud/retrying_file_system.h
index 636e2ba08f..d9d8ea6b00 100644
--- a/tensorflow/core/platform/cloud/retrying_file_system.h
+++ b/tensorflow/core/platform/cloud/retrying_file_system.h
@@ -45,7 +45,7 @@ class RetryingFileSystem : public FileSystem {
const string& filename,
std::unique_ptr<ReadOnlyMemoryRegion>* result) override;
- bool FileExists(const string& fname) override;
+ Status FileExists(const string& fname) override;
Status GetChildren(const string& dir, std::vector<string>* result) override;
diff --git a/tensorflow/core/platform/cloud/retrying_file_system_test.cc b/tensorflow/core/platform/cloud/retrying_file_system_test.cc
index 06c16b9092..ccb9def3cb 100644
--- a/tensorflow/core/platform/cloud/retrying_file_system_test.cc
+++ b/tensorflow/core/platform/cloud/retrying_file_system_test.cc
@@ -100,7 +100,7 @@ class MockFileSystem : public FileSystem {
return calls_.ConsumeNextCall("NewReadOnlyMemoryRegionFromFile");
}
- bool FileExists(const string& fname) override { return true; }
+ Status FileExists(const string& fname) override { return Status::OK(); }
Status GetChildren(const string& dir, std::vector<string>* result) override {
return calls_.ConsumeNextCall("GetChildren");
diff --git a/tensorflow/core/platform/env.cc b/tensorflow/core/platform/env.cc
index 5a09fded9b..114fef1489 100644
--- a/tensorflow/core/platform/env.cc
+++ b/tensorflow/core/platform/env.cc
@@ -117,11 +117,9 @@ Status Env::NewAppendableFile(const string& fname,
return fs->NewAppendableFile(fname, result);
}
-bool Env::FileExists(const string& fname) {
+Status Env::FileExists(const string& fname) {
FileSystem* fs;
- if (!GetFileSystemForFile(fname, &fs).ok()) {
- return false;
- }
+ TF_RETURN_IF_ERROR(GetFileSystemForFile(fname, &fs));
return fs->FileExists(fname);
}
diff --git a/tensorflow/core/platform/env.h b/tensorflow/core/platform/env.h
index 3aaf3a56f7..fbda05ade1 100644
--- a/tensorflow/core/platform/env.h
+++ b/tensorflow/core/platform/env.h
@@ -132,8 +132,8 @@ class Env {
Status NewReadOnlyMemoryRegionFromFile(
const string& fname, std::unique_ptr<ReadOnlyMemoryRegion>* result);
- /// Returns true iff the named file exists.
- bool FileExists(const string& fname);
+ /// Returns OK if the named path exists and NOT_FOUND otherwise.
+ Status FileExists(const string& fname);
/// \brief Stores in *result the names of the children of the specified
/// directory. The names are relative to "dir".
diff --git a/tensorflow/core/platform/env_test.cc b/tensorflow/core/platform/env_test.cc
index f6fa27327a..52ced38ac8 100644
--- a/tensorflow/core/platform/env_test.cc
+++ b/tensorflow/core/platform/env_test.cc
@@ -161,10 +161,10 @@ TEST_F(DefaultEnvTest, DeleteRecursively) {
env_->DeleteRecursively(parent_dir, &undeleted_files, &undeleted_dirs));
EXPECT_EQ(0, undeleted_files);
EXPECT_EQ(0, undeleted_dirs);
- EXPECT_FALSE(env_->FileExists(root_file1));
- EXPECT_FALSE(env_->FileExists(root_file2));
- EXPECT_FALSE(env_->FileExists(root_file3));
- EXPECT_FALSE(env_->FileExists(child1_file1));
+ EXPECT_EQ(error::Code::NOT_FOUND, env_->FileExists(root_file1).code());
+ EXPECT_EQ(error::Code::NOT_FOUND, env_->FileExists(root_file2).code());
+ EXPECT_EQ(error::Code::NOT_FOUND, env_->FileExists(root_file3).code());
+ EXPECT_EQ(error::Code::NOT_FOUND, env_->FileExists(child1_file1).code());
}
TEST_F(DefaultEnvTest, DeleteRecursivelyFail) {
@@ -174,7 +174,7 @@ TEST_F(DefaultEnvTest, DeleteRecursivelyFail) {
int64 undeleted_files, undeleted_dirs;
Status s =
env_->DeleteRecursively(parent_dir, &undeleted_files, &undeleted_dirs);
- EXPECT_EQ("Not found: Directory doesn't exist", s.ToString());
+ EXPECT_EQ(error::Code::NOT_FOUND, s.code());
EXPECT_EQ(0, undeleted_files);
EXPECT_EQ(1, undeleted_dirs);
}
@@ -183,7 +183,7 @@ TEST_F(DefaultEnvTest, RecursivelyCreateDir) {
const string create_path = io::JoinPath(BaseDir(), "a//b/c/d");
TF_CHECK_OK(env_->RecursivelyCreateDir(create_path));
TF_CHECK_OK(env_->RecursivelyCreateDir(create_path)); // repeat creation.
- EXPECT_TRUE(env_->FileExists(create_path));
+ TF_EXPECT_OK(env_->FileExists(create_path));
}
TEST_F(DefaultEnvTest, RecursivelyCreateDirEmpty) {
@@ -195,14 +195,14 @@ TEST_F(DefaultEnvTest, RecursivelyCreateDirSubdirsExist) {
const string subdir_path = io::JoinPath(BaseDir(), "a/b");
TF_CHECK_OK(env_->CreateDir(io::JoinPath(BaseDir(), "a")));
TF_CHECK_OK(env_->CreateDir(subdir_path));
- EXPECT_TRUE(env_->FileExists(subdir_path));
+ TF_EXPECT_OK(env_->FileExists(subdir_path));
// Now try to recursively create a/b/c/d/
const string create_path = io::JoinPath(BaseDir(), "a/b/c/d/");
TF_CHECK_OK(env_->RecursivelyCreateDir(create_path));
TF_CHECK_OK(env_->RecursivelyCreateDir(create_path)); // repeat creation.
- EXPECT_TRUE(env_->FileExists(create_path));
- EXPECT_TRUE(env_->FileExists(io::JoinPath(BaseDir(), "a/b/c")));
+ TF_EXPECT_OK(env_->FileExists(create_path));
+ TF_EXPECT_OK(env_->FileExists(io::JoinPath(BaseDir(), "a/b/c")));
}
TEST_F(DefaultEnvTest, LocalFileSystem) {
@@ -243,10 +243,10 @@ TEST_F(DefaultEnvTest, SleepForMicroseconds) {
class TmpDirFileSystem : public NullFileSystem {
public:
- bool FileExists(const string& dir) override {
+ Status FileExists(const string& dir) override {
StringPiece scheme, host, path;
io::ParseURI(dir, &scheme, &host, &path);
- if (path.empty()) return false;
+ if (path.empty()) return errors::NotFound(dir, " not found");
return Env::Default()->FileExists(io::JoinPath(BaseDir(), path));
}
@@ -268,10 +268,10 @@ REGISTER_FILE_SYSTEM("tmpdirfs", TmpDirFileSystem);
TEST_F(DefaultEnvTest, RecursivelyCreateDirWithUri) {
Env* env = Env::Default();
const string create_path = "tmpdirfs://testhost/a/b/c/d";
- EXPECT_FALSE(env->FileExists(create_path));
+ EXPECT_EQ(error::Code::NOT_FOUND, env->FileExists(create_path).code());
TF_CHECK_OK(env->RecursivelyCreateDir(create_path));
TF_CHECK_OK(env->RecursivelyCreateDir(create_path)); // repeat creation.
- EXPECT_TRUE(env->FileExists(create_path));
+ TF_EXPECT_OK(env->FileExists(create_path));
}
} // namespace tensorflow
diff --git a/tensorflow/core/platform/file_system.cc b/tensorflow/core/platform/file_system.cc
index 6f55880a1d..1692398b97 100644
--- a/tensorflow/core/platform/file_system.cc
+++ b/tensorflow/core/platform/file_system.cc
@@ -61,9 +61,7 @@ string FileSystem::TranslateName(const string& name) const {
Status FileSystem::IsDirectory(const string& name) {
// Check if path exists.
- if (!FileExists(name)) {
- return Status(tensorflow::error::NOT_FOUND, "Path not found");
- }
+ TF_RETURN_IF_ERROR(FileExists(name));
FileStatistics stat;
TF_RETURN_IF_ERROR(Stat(name, &stat));
if (stat.is_directory) {
@@ -143,9 +141,10 @@ Status FileSystem::DeleteRecursively(const string& dirname,
*undeleted_files = 0;
*undeleted_dirs = 0;
// Make sure that dirname exists;
- if (!FileExists(dirname)) {
+ Status exists_status = FileExists(dirname);
+ if (!exists_status.ok()) {
(*undeleted_dirs)++;
- return Status(error::NOT_FOUND, "Directory doesn't exist");
+ return exists_status;
}
std::deque<string> dir_q; // Queue for the BFS
std::vector<string> dir_list; // List of all dirs discovered
@@ -201,8 +200,14 @@ Status FileSystem::RecursivelyCreateDir(const string& dirname) {
StringPiece scheme, host, remaining_dir;
io::ParseURI(dirname, &scheme, &host, &remaining_dir);
std::vector<StringPiece> sub_dirs;
- while (!FileExists(io::CreateURI(scheme, host, remaining_dir)) &&
- !remaining_dir.empty()) {
+ while (!remaining_dir.empty()) {
+ Status status = FileExists(io::CreateURI(scheme, host, remaining_dir));
+ if (status.ok()) {
+ break;
+ }
+ if (status.code() != error::Code::NOT_FOUND) {
+ return status;
+ }
// Basename returns "" for / ending dirs.
if (!remaining_dir.ends_with("/")) {
sub_dirs.push_back(io::Basename(remaining_dir));
diff --git a/tensorflow/core/platform/file_system.h b/tensorflow/core/platform/file_system.h
index dfaf75be66..78453c4f72 100644
--- a/tensorflow/core/platform/file_system.h
+++ b/tensorflow/core/platform/file_system.h
@@ -61,7 +61,7 @@ class FileSystem {
virtual Status NewReadOnlyMemoryRegionFromFile(
const string& fname, std::unique_ptr<ReadOnlyMemoryRegion>* result) = 0;
- virtual bool FileExists(const string& fname) = 0;
+ virtual Status FileExists(const string& fname) = 0;
/// \brief Returns the immediate children in the given directory.
///
@@ -178,7 +178,9 @@ class NullFileSystem : public FileSystem {
"NewReadOnlyMemoryRegionFromFile unimplemented");
}
- bool FileExists(const string& fname) override { return false; }
+ Status FileExists(const string& fname) override {
+ return errors::Unimplemented("FileExists unimplemented");
+ }
Status GetChildren(const string& dir, std::vector<string>* result) override {
return errors::Unimplemented("GetChildren unimplemented");
diff --git a/tensorflow/core/platform/file_system_test.cc b/tensorflow/core/platform/file_system_test.cc
index 8cdabdc8bc..007ac6c15d 100644
--- a/tensorflow/core/platform/file_system_test.cc
+++ b/tensorflow/core/platform/file_system_test.cc
@@ -31,10 +31,13 @@ static const char* const kPrefix = "ipfs://solarsystem";
// cannot have children further.
class InterPlanetaryFileSystem : public NullFileSystem {
public:
- bool FileExists(const string& fname) override {
+ Status FileExists(const string& fname) override {
string parsed_path;
ParsePath(fname, &parsed_path);
- return BodyExists(parsed_path);
+ if (BodyExists(parsed_path)) {
+ return Status::OK();
+ }
+ return Status(tensorflow::error::NOT_FOUND, "File does not exist");
}
// Adds the dir to the parent's children list and creates an entry for itself.
diff --git a/tensorflow/core/platform/hadoop/hadoop_file_system.cc b/tensorflow/core/platform/hadoop/hadoop_file_system.cc
index 749d9e1fcd..7ae63ac1d2 100644
--- a/tensorflow/core/platform/hadoop/hadoop_file_system.cc
+++ b/tensorflow/core/platform/hadoop/hadoop_file_system.cc
@@ -290,15 +290,13 @@ Status HadoopFileSystem::NewReadOnlyMemoryRegionFromFile(
return errors::Unimplemented("HDFS does not support ReadOnlyMemoryRegion");
}
-bool HadoopFileSystem::FileExists(const string& fname) {
+Status HadoopFileSystem::FileExists(const string& fname) {
hdfsFS fs = nullptr;
- Status status = Connect(fname, &fs);
- if (!status.ok()) {
- LOG(ERROR) << "Connect failed: " << status.error_message();
- return false;
+ TF_RETURN_IF_ERROR(Connect(fname, &fs));
+ if (hdfs_->hdfsExists(fs, TranslateName(fname).c_str()) == 0) {
+ return Status::OK();
}
-
- return hdfs_->hdfsExists(fs, TranslateName(fname).c_str()) == 0;
+ return errors::NotFound(fname, " not found.");
}
Status HadoopFileSystem::GetChildren(const string& dir,
diff --git a/tensorflow/core/platform/hadoop/hadoop_file_system.h b/tensorflow/core/platform/hadoop/hadoop_file_system.h
index 182d6995fc..447e83158a 100644
--- a/tensorflow/core/platform/hadoop/hadoop_file_system.h
+++ b/tensorflow/core/platform/hadoop/hadoop_file_system.h
@@ -45,7 +45,7 @@ class HadoopFileSystem : public FileSystem {
const string& fname,
std::unique_ptr<ReadOnlyMemoryRegion>* result) override;
- bool FileExists(const string& fname) override;
+ Status FileExists(const string& fname) override;
Status GetChildren(const string& dir, std::vector<string>* result) override;
diff --git a/tensorflow/core/platform/hadoop/hadoop_file_system_test.cc b/tensorflow/core/platform/hadoop/hadoop_file_system_test.cc
index 997ad1674c..cb12913f13 100644
--- a/tensorflow/core/platform/hadoop/hadoop_file_system_test.cc
+++ b/tensorflow/core/platform/hadoop/hadoop_file_system_test.cc
@@ -100,9 +100,9 @@ TEST_F(HadoopFileSystemTest, WritableFile) {
TEST_F(HadoopFileSystemTest, FileExists) {
const string fname =
"file://" + io::JoinPath(testing::TmpDir(), "FileExists");
- EXPECT_FALSE(hdfs.FileExists(fname));
+ EXPECT_EQ(error::Code::NOT_FOUND, hdfs.FileExists(fname).code());
TF_ASSERT_OK(WriteString(fname, "test"));
- EXPECT_TRUE(hdfs.FileExists(fname));
+ TF_EXPECT_OK(hdfs.FileExists(fname));
}
TEST_F(HadoopFileSystemTest, GetChildren) {
@@ -175,7 +175,7 @@ TEST_F(HadoopFileSystemTest, RenameFile_Overwrite) {
"file://" + io::JoinPath(testing::TmpDir(), "RenameFile2");
TF_ASSERT_OK(WriteString(fname2, "test"));
- EXPECT_TRUE(hdfs.FileExists(fname2));
+ TF_EXPECT_OK(hdfs.FileExists(fname2));
TF_ASSERT_OK(WriteString(fname1, "test"));
TF_EXPECT_OK(hdfs.RenameFile(fname1, fname2));
diff --git a/tensorflow/core/platform/posix/posix_file_system.cc b/tensorflow/core/platform/posix/posix_file_system.cc
index 4bd8d84d6c..00560d79c1 100644
--- a/tensorflow/core/platform/posix/posix_file_system.cc
+++ b/tensorflow/core/platform/posix/posix_file_system.cc
@@ -191,8 +191,11 @@ Status PosixFileSystem::NewReadOnlyMemoryRegionFromFile(
return s;
}
-bool PosixFileSystem::FileExists(const string& fname) {
- return access(TranslateName(fname).c_str(), F_OK) == 0;
+Status PosixFileSystem::FileExists(const string& fname) {
+ if (access(TranslateName(fname).c_str(), F_OK) == 0) {
+ return Status::OK();
+ }
+ return errors::NotFound(fname, " not found");
}
Status PosixFileSystem::GetChildren(const string& dir,
diff --git a/tensorflow/core/platform/posix/posix_file_system.h b/tensorflow/core/platform/posix/posix_file_system.h
index ccff70cb56..fe050fd5a0 100644
--- a/tensorflow/core/platform/posix/posix_file_system.h
+++ b/tensorflow/core/platform/posix/posix_file_system.h
@@ -41,7 +41,7 @@ class PosixFileSystem : public FileSystem {
const string& filename,
std::unique_ptr<ReadOnlyMemoryRegion>* result) override;
- bool FileExists(const string& fname) override;
+ Status FileExists(const string& fname) override;
Status GetChildren(const string& dir, std::vector<string>* result) override;
diff --git a/tensorflow/core/platform/windows/windows_file_system.cc b/tensorflow/core/platform/windows/windows_file_system.cc
index 714bb55d50..c6c42f0150 100644
--- a/tensorflow/core/platform/windows/windows_file_system.cc
+++ b/tensorflow/core/platform/windows/windows_file_system.cc
@@ -371,9 +371,12 @@ Status WindowsFileSystem::NewReadOnlyMemoryRegionFromFile(
return s;
}
-bool WindowsFileSystem::FileExists(const string& fname) {
+Status WindowsFileSystem::FileExists(const string& fname) {
constexpr int kOk = 0;
- return _access(TranslateName(fname).c_str(), kOk) == 0;
+ if (_access(TranslateName(fname).c_str(), kOk) == 0) {
+ return Status::OK();
+ }
+ return errors::NotFound(fname, " not found");
}
Status WindowsFileSystem::GetChildren(const string& dir,
@@ -477,4 +480,4 @@ Status WindowsFileSystem::Stat(const string& fname, FileStatistics* stat) {
return result;
}
-} // namespace tensorflow \ No newline at end of file
+} // namespace tensorflow
diff --git a/tensorflow/core/platform/windows/windows_file_system.h b/tensorflow/core/platform/windows/windows_file_system.h
index 64da239d96..dd83a27caf 100644
--- a/tensorflow/core/platform/windows/windows_file_system.h
+++ b/tensorflow/core/platform/windows/windows_file_system.h
@@ -44,7 +44,7 @@ class WindowsFileSystem : public FileSystem {
const string& fname,
std::unique_ptr<ReadOnlyMemoryRegion>* result) override;
- bool FileExists(const string& fname) override;
+ Status FileExists(const string& fname) override;
Status GetChildren(const string& dir, std::vector<string>* result) override;
diff --git a/tensorflow/core/util/events_writer.cc b/tensorflow/core/util/events_writer.cc
index 2a810f641e..ee33722718 100644
--- a/tensorflow/core/util/events_writer.cc
+++ b/tensorflow/core/util/events_writer.cc
@@ -154,7 +154,7 @@ bool EventsWriter::Close() {
}
bool EventsWriter::FileHasDisappeared() {
- if (env_->FileExists(filename_)) {
+ if (env_->FileExists(filename_).ok()) {
return false;
} else {
// This can happen even with non-null recordio_writer_ if some other
diff --git a/tensorflow/core/util/events_writer_test.cc b/tensorflow/core/util/events_writer_test.cc
index 943dd46104..a96be61985 100644
--- a/tensorflow/core/util/events_writer_test.cc
+++ b/tensorflow/core/util/events_writer_test.cc
@@ -61,7 +61,7 @@ static bool ReadEventProto(io::RecordReader* reader, uint64* offset,
}
void VerifyFile(const string& filename) {
- CHECK(env()->FileExists(filename));
+ CHECK(env()->FileExists(filename).ok());
std::unique_ptr<RandomAccessFile> event_file;
TF_CHECK_OK(env()->NewRandomAccessFile(filename, &event_file));
io::RecordReader* reader = new io::RecordReader(event_file.get());
@@ -139,11 +139,11 @@ TEST(EventWriter, FailFlush) {
EventsWriter writer(file_prefix);
string filename = writer.FileName();
WriteFile(&writer);
- EXPECT_TRUE(env()->FileExists(filename));
+ TF_EXPECT_OK(env()->FileExists(filename));
env()->DeleteFile(filename);
- EXPECT_FALSE(env()->FileExists(filename));
+ EXPECT_EQ(errors::Code::NOT_FOUND, env()->FileExists(filename).code());
EXPECT_FALSE(writer.Flush());
- EXPECT_FALSE(env()->FileExists(filename));
+ EXPECT_EQ(errors::Code::NOT_FOUND, env()->FileExists(filename).code());
}
TEST(EventWriter, FailClose) {
@@ -151,11 +151,11 @@ TEST(EventWriter, FailClose) {
EventsWriter writer(file_prefix);
string filename = writer.FileName();
WriteFile(&writer);
- EXPECT_TRUE(env()->FileExists(filename));
+ TF_EXPECT_OK(env()->FileExists(filename));
env()->DeleteFile(filename);
- EXPECT_FALSE(env()->FileExists(filename));
+ EXPECT_EQ(errors::Code::NOT_FOUND, env()->FileExists(filename).code());
EXPECT_FALSE(writer.Close());
- EXPECT_FALSE(env()->FileExists(filename));
+ EXPECT_EQ(errors::Code::NOT_FOUND, env()->FileExists(filename).code());
}
TEST(EventWriter, InitWriteClose) {
@@ -163,7 +163,7 @@ TEST(EventWriter, InitWriteClose) {
EventsWriter writer(file_prefix);
EXPECT_TRUE(writer.Init());
string filename0 = writer.FileName();
- EXPECT_TRUE(env()->FileExists(filename0));
+ TF_EXPECT_OK(env()->FileExists(filename0));
WriteFile(&writer);
EXPECT_TRUE(writer.Close());
string filename1 = writer.FileName();
@@ -175,7 +175,7 @@ TEST(EventWriter, NameWriteClose) {
string file_prefix = GetDirName("/namewriteclose_test");
EventsWriter writer(file_prefix);
string filename = writer.FileName();
- EXPECT_TRUE(env()->FileExists(filename));
+ TF_EXPECT_OK(env()->FileExists(filename));
WriteFile(&writer);
EXPECT_TRUE(writer.Close());
VerifyFile(filename);
@@ -186,7 +186,7 @@ TEST(EventWriter, NameClose) {
EventsWriter writer(file_prefix);
string filename = writer.FileName();
EXPECT_TRUE(writer.Close());
- EXPECT_TRUE(env()->FileExists(filename));
+ TF_EXPECT_OK(env()->FileExists(filename));
env()->DeleteFile(filename);
}
@@ -194,7 +194,7 @@ TEST(EventWriter, FileDeletionBeforeWriting) {
string file_prefix = GetDirName("/fdbw_test");
EventsWriter writer(file_prefix);
string filename0 = writer.FileName();
- EXPECT_TRUE(env()->FileExists(filename0));
+ TF_EXPECT_OK(env()->FileExists(filename0));
env()->SleepForMicroseconds(
2000000); // To make sure timestamp part of filename will differ.
env()->DeleteFile(filename0);
diff --git a/tensorflow/core/util/memmapped_file_system.cc b/tensorflow/core/util/memmapped_file_system.cc
index fa0353f541..d67f948f1d 100644
--- a/tensorflow/core/util/memmapped_file_system.cc
+++ b/tensorflow/core/util/memmapped_file_system.cc
@@ -79,12 +79,15 @@ class RandomAccessFileFromMemmapped : public RandomAccessFile {
MemmappedFileSystem::MemmappedFileSystem() {}
-bool MemmappedFileSystem::FileExists(const string& fname) {
+Status MemmappedFileSystem::FileExists(const string& fname) {
if (!mapped_memory_) {
- return false;
+ return errors::FailedPrecondition("MemmappedEnv is not initialized");
}
const auto dir_element = directory_.find(fname);
- return dir_element != directory_.end();
+ if (dir_element != directory_.end()) {
+ return Status::OK();
+ }
+ return errors::NotFound(fname, " not found");
}
Status MemmappedFileSystem::NewRandomAccessFile(
diff --git a/tensorflow/core/util/memmapped_file_system.h b/tensorflow/core/util/memmapped_file_system.h
index c851ee77e6..d64c4a765c 100644
--- a/tensorflow/core/util/memmapped_file_system.h
+++ b/tensorflow/core/util/memmapped_file_system.h
@@ -60,7 +60,7 @@ class MemmappedFileSystem : public FileSystem {
MemmappedFileSystem();
~MemmappedFileSystem() override = default;
- bool FileExists(const string& fname) override;
+ Status FileExists(const string& fname) override;
Status NewRandomAccessFile(
const string& filename,
std::unique_ptr<RandomAccessFile>* result) override;
diff --git a/tensorflow/core/util/memmapped_file_system_test.cc b/tensorflow/core/util/memmapped_file_system_test.cc
index a209ef8b9b..c7d919041a 100644
--- a/tensorflow/core/util/memmapped_file_system_test.cc
+++ b/tensorflow/core/util/memmapped_file_system_test.cc
@@ -102,8 +102,9 @@ TEST(MemmappedFileSystemTest, SimpleTest) {
.code());
// Check FileExists.
- EXPECT_TRUE(memmapped_env.FileExists(kTensor2FileName));
- EXPECT_FALSE(memmapped_env.FileExists("bla-bla-bla"));
+ TF_EXPECT_OK(memmapped_env.FileExists(kTensor2FileName));
+ EXPECT_EQ(error::Code::NOT_FOUND,
+ memmapped_env.FileExists("bla-bla-bla").code());
}
TEST(MemmappedFileSystemTest, NotInitalized) {
diff --git a/tensorflow/core/util/reporter.cc b/tensorflow/core/util/reporter.cc
index a7e4b48175..c4896fe2ac 100644
--- a/tensorflow/core/util/reporter.cc
+++ b/tensorflow/core/util/reporter.cc
@@ -54,7 +54,7 @@ Status TestReporter::Initialize() {
string mangled_fname = strings::StrCat(
fname_, str_util::Join(str_util::Split(test_name_, '/'), "__"));
Env* env = Env::Default();
- if (env->FileExists(mangled_fname)) {
+ if (env->FileExists(mangled_fname).ok()) {
return errors::InvalidArgument("Cannot create TestReporter, file exists: ",
mangled_fname);
}
diff --git a/tensorflow/core/util/tensor_bundle/tensor_bundle_test.cc b/tensorflow/core/util/tensor_bundle/tensor_bundle_test.cc
index a06f0cf782..2ffe186e12 100644
--- a/tensorflow/core/util/tensor_bundle/tensor_bundle_test.cc
+++ b/tensorflow/core/util/tensor_bundle/tensor_bundle_test.cc
@@ -364,7 +364,7 @@ TEST(TensorBundleTest, DirectoryStructure) {
gtl::ArraySlice<string> expected_files) {
StringPiece dir = io::Dirname(bundle_prefix);
for (const string& expected_file : expected_files) {
- EXPECT_TRUE(env->FileExists(io::JoinPath(dir, expected_file)));
+ TF_EXPECT_OK(env->FileExists(io::JoinPath(dir, expected_file)));
}
};
diff --git a/tensorflow/python/lib/io/file_io.i b/tensorflow/python/lib/io/file_io.i
index 33a0ef6492..4d72832b05 100644
--- a/tensorflow/python/lib/io/file_io.i
+++ b/tensorflow/python/lib/io/file_io.i
@@ -32,12 +32,20 @@ limitations under the License.
%}
%{
-inline bool FileExists(const string& filename) {
- return tensorflow::Env::Default()->FileExists(filename);
+inline void FileExists(const string& filename, TF_Status* out_status) {
+ tensorflow::Status status = tensorflow::Env::Default()->FileExists(filename);
+ if (!status.ok()) {
+ Set_TF_Status_from_Status(out_status, status);
+ }
}
-inline bool FileExists(const tensorflow::StringPiece& filename) {
- return tensorflow::Env::Default()->FileExists(filename.ToString());
+inline void FileExists(const tensorflow::StringPiece& filename,
+ TF_Status* out_status) {
+ tensorflow::Status status =
+ tensorflow::Env::Default()->FileExists(filename.ToString());
+ if (!status.ok()) {
+ Set_TF_Status_from_Status(out_status, status);
+ }
}
inline void DeleteFile(const string& filename, TF_Status* out_status) {
@@ -105,7 +113,7 @@ void RecursivelyCreateDir(const string& dirname, TF_Status* out_status) {
void CopyFile(const string& oldpath, const string& newpath, bool overwrite,
TF_Status* out_status) {
// If overwrite is false and the newpath file exists then it's an error.
- if (!overwrite && FileExists(newpath)) {
+ if (!overwrite && tensorflow::Env::Default()->FileExists(newpath).ok()) {
TF_SetStatus(out_status, TF_ALREADY_EXISTS, "file already exists");
return;
}
@@ -125,7 +133,7 @@ void CopyFile(const string& oldpath, const string& newpath, bool overwrite,
void RenameFile(const string& src, const string& target, bool overwrite,
TF_Status* out_status) {
// If overwrite is false and the target file exists then its an error.
- if (!overwrite && FileExists(target)) {
+ if (!overwrite && tensorflow::Env::Default()->FileExists(target).ok()) {
TF_SetStatus(out_status, TF_ALREADY_EXISTS, "file already exists");
return;
}
@@ -238,7 +246,7 @@ string ReadFromStream(tensorflow::io::BufferedInputStream* stream,
%newobject CreateWritableFile;
// Wrap the above functions.
-inline bool FileExists(const string& filename);
+inline void FileExists(const string& filename, TF_Status* out_status);
inline void DeleteFile(const string& filename, TF_Status* out_status);
string ReadFileToString(const string& filename, TF_Status* out_status);
void WriteStringToFile(const string& filename, const string& file_content,
diff --git a/tensorflow/python/lib/io/file_io.py b/tensorflow/python/lib/io/file_io.py
index adce569019..2653219c5a 100644
--- a/tensorflow/python/lib/io/file_io.py
+++ b/tensorflow/python/lib/io/file_io.py
@@ -191,8 +191,17 @@ def file_exists(filename):
Returns:
True if the path exists, whether its a file or a directory.
+ False if the path does not exist and there are no filesystem errors.
+
+ Raises:
+ errors.OpError: Propagates any errors reported by the FileSystem API.
"""
- return pywrap_tensorflow.FileExists(compat.as_bytes(filename))
+ try:
+ with errors.raise_exception_on_not_ok_status() as status:
+ pywrap_tensorflow.FileExists(compat.as_bytes(filename), status)
+ except errors.NotFoundError:
+ return False
+ return True
def delete_file(filename):
diff --git a/tensorflow/stream_executor/cuda/cuda_gpu_executor.cc b/tensorflow/stream_executor/cuda/cuda_gpu_executor.cc
index f69853df99..322d33ae26 100644
--- a/tensorflow/stream_executor/cuda/cuda_gpu_executor.cc
+++ b/tensorflow/stream_executor/cuda/cuda_gpu_executor.cc
@@ -177,7 +177,7 @@ bool CUDAExecutor::FindOnDiskForComputeCapability(
// have been migrated.
string cc_specific = port::StrCat(filename.ToString(), ".cc", cc_major_,
cc_minor_, canonical_suffix.ToString());
- if (port::FileExists(cc_specific)) {
+ if (port::FileExists(cc_specific).ok()) {
VLOG(2) << "found compute-capability-specific file, using that: "
<< cc_specific;
*found_filename = cc_specific;
@@ -186,7 +186,7 @@ bool CUDAExecutor::FindOnDiskForComputeCapability(
VLOG(2) << "could not find compute-capability specific file at: "
<< cc_specific;
- if (port::FileExists(filename.ToString())) {
+ if (port::FileExists(filename.ToString()).ok()) {
*found_filename = filename.ToString();
return true;
}
diff --git a/tensorflow/stream_executor/lib/env.h b/tensorflow/stream_executor/lib/env.h
index 068fe7936f..f11a4cb25b 100644
--- a/tensorflow/stream_executor/lib/env.h
+++ b/tensorflow/stream_executor/lib/env.h
@@ -29,11 +29,11 @@ using tensorflow::ReadFileToString;
using tensorflow::Thread;
using tensorflow::WriteStringToFile;
-inline bool FileExists(const string& filename) {
+inline Status FileExists(const string& filename) {
return Env::Default()->FileExists(filename);
}
-inline bool FileExists(const port::StringPiece& filename) {
+inline Status FileExists(const port::StringPiece& filename) {
return Env::Default()->FileExists(filename.ToString());
}