aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/platform/env.cc
diff options
context:
space:
mode:
authorGravatar Rohan Jain <rohanj@google.com>2016-09-29 15:41:30 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-09-29 16:48:02 -0700
commit4a3065a1b796e8c5b52987cc3442ea3d529369c1 (patch)
tree707fb9755cf4d5137333b64c0d374bfdecd44aba /tensorflow/core/platform/env.cc
parent77b1a6cf737c85920a5f12b28caf5d5f0ed50b48 (diff)
Moving implementation of RecursivelyCreateDir and DeleteRecursively down to the FileSystem from the Env so that file systems could override if needed.
Change: 134725784
Diffstat (limited to 'tensorflow/core/platform/env.cc')
-rw-r--r--tensorflow/core/platform/env.cc82
1 files changed, 2 insertions, 80 deletions
diff --git a/tensorflow/core/platform/env.cc b/tensorflow/core/platform/env.cc
index 653d1103c7..e4bd54cbb6 100644
--- a/tensorflow/core/platform/env.cc
+++ b/tensorflow/core/platform/env.cc
@@ -147,28 +147,7 @@ Status Env::DeleteFile(const string& fname) {
Status Env::RecursivelyCreateDir(const string& dirname) {
FileSystem* fs;
TF_RETURN_IF_ERROR(GetFileSystemForFile(dirname, &fs));
- StringPiece scheme, host, remaining_dir;
- ParseURI(dirname, &scheme, &host, &remaining_dir);
- std::vector<StringPiece> sub_dirs;
- while (!fs->FileExists(CreateURI(scheme, host, remaining_dir)) &&
- !remaining_dir.empty()) {
- // Basename returns "" for / ending dirs.
- if (!remaining_dir.ends_with("/")) {
- sub_dirs.push_back(io::Basename(remaining_dir));
- }
- remaining_dir = io::Dirname(remaining_dir);
- }
-
- // sub_dirs contains all the dirs to be created but in reverse order.
- std::reverse(sub_dirs.begin(), sub_dirs.end());
-
- // Now create the directories.
- string built_path = remaining_dir.ToString();
- for (const StringPiece sub_dir : sub_dirs) {
- built_path = io::JoinPath(built_path, sub_dir);
- TF_RETURN_IF_ERROR(fs->CreateDir(CreateURI(scheme, host, built_path)));
- }
- return Status::OK();
+ return fs->RecursivelyCreateDir(dirname);
}
Status Env::CreateDir(const string& dirname) {
@@ -197,66 +176,9 @@ Status Env::IsDirectory(const string& fname) {
Status Env::DeleteRecursively(const string& dirname, int64* undeleted_files,
int64* undeleted_dirs) {
- CHECK_NOTNULL(undeleted_files);
- CHECK_NOTNULL(undeleted_dirs);
FileSystem* fs;
TF_RETURN_IF_ERROR(GetFileSystemForFile(dirname, &fs));
-
- *undeleted_files = 0;
- *undeleted_dirs = 0;
- // Make sure that dirname exists;
- if (!FileExists(dirname)) {
- (*undeleted_dirs)++;
- return Status(error::NOT_FOUND, "Directory doesn't exist");
- }
- std::deque<string> dir_q; // Queue for the BFS
- std::vector<string> dir_list; // List of all dirs discovered
- dir_q.push_back(dirname);
- Status ret; // Status to be returned.
- // Do a BFS on the directory to discover all the sub-directories. Remove all
- // children that are files along the way. Then cleanup and remove the
- // directories in reverse order.;
- while (!dir_q.empty()) {
- string dir = dir_q.front();
- dir_q.pop_front();
- dir_list.push_back(dir);
- std::vector<string> children;
- // GetChildren might fail if we don't have appropriate permissions.
- Status s = fs->GetChildren(dir, &children);
- ret.Update(s);
- if (!s.ok()) {
- (*undeleted_dirs)++;
- continue;
- }
- for (const string& child : children) {
- const string child_path = io::JoinPath(dir, child);
- // If the child is a directory add it to the queue, otherwise delete it.
- if (fs->IsDirectory(child_path).ok()) {
- dir_q.push_back(child_path);
- } else {
- // Delete file might fail because of permissions issues or might be
- // unimplemented.
- Status del_status = fs->DeleteFile(child_path);
- ret.Update(del_status);
- if (!del_status.ok()) {
- (*undeleted_files)++;
- }
- }
- }
- }
- // Now reverse the list of directories and delete them. The BFS ensures that
- // we can delete the directories in this order.
- std::reverse(dir_list.begin(), dir_list.end());
- for (const string& dir : dir_list) {
- // Delete dir might fail because of permissions issues or might be
- // unimplemented.
- Status s = fs->DeleteDir(dir);
- ret.Update(s);
- if (!s.ok()) {
- (*undeleted_dirs)++;
- }
- }
- return Status::OK();
+ return fs->DeleteRecursively(dirname, undeleted_files, undeleted_dirs);
}
Status Env::GetFileSize(const string& fname, uint64* file_size) {