Skip to content

Commit

Permalink
util: create directories if missing for backup/restore
Browse files Browse the repository at this point in the history
Signed-off-by: Hans Holmberg <[email protected]>
  • Loading branch information
yhr committed Feb 18, 2022
1 parent df8c264 commit 398f7f4
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions util/zenfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,24 @@ IOStatus zenfs_tool_copy_dir(FileSystem *f_fs, const std::string &f_dir,

return s;
}
IOStatus zenfs_create_directories(FileSystem *fs, std::string path) {
std::string dir_name;
IODebugContext dbg;
IOOptions opts;
IOStatus s;
std::size_t p = 0;

if (path.back() != '/') path += '/';

while ((p = path.find_first_of('/', p)) != std::string::npos) {
dir_name = path.substr(0, p++);
if (dir_name.size() == 0) continue;
s = fs->CreateDirIfMissing(dir_name, opts, &dbg);
if (!s.ok()) break;
}

return s;
}

int zenfs_tool_backup() {
Status status;
Expand Down Expand Up @@ -511,6 +529,14 @@ int zenfs_tool_backup() {
zenfs_tool_copy_file(zenFS.get(), FLAGS_backup_path,
FileSystem::Default().get(), dest_filename);
} else {
io_status =
zenfs_create_directories(FileSystem::Default().get(), FLAGS_path);
if (!io_status.ok()) {
fprintf(stderr, "Create directory failed, error: %s\n",
io_status.ToString().c_str());
return 1;
}

std::string backup_path = FLAGS_backup_path;
if (backup_path.size() > 0 && backup_path.back() != '/') backup_path += "/";
io_status = zenfs_tool_copy_dir(zenFS.get(), backup_path,
Expand Down Expand Up @@ -550,6 +576,13 @@ int zenfs_tool_restore() {
std::string path = FLAGS_path;
if (path.back() != '/') path += "/";

io_status = zenfs_create_directories(zenFS.get(), FLAGS_restore_path);
if (!io_status.ok()) {
fprintf(stderr, "Create directory failed, error: %s\n",
io_status.ToString().c_str());
return 1;
}

io_status = zenfs_tool_copy_dir(FileSystem::Default().get(), path,
zenFS.get(), FLAGS_restore_path);
if (!io_status.ok()) {
Expand Down

0 comments on commit 398f7f4

Please sign in to comment.