Skip to content

Commit

Permalink
Refactor file copy method
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryKogan committed Dec 20, 2023
1 parent f2da641 commit 5bdcdc6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/FileSystem/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,12 @@ auto FileSystem::rm(std::string const &path, bool recursive) -> void {
}

auto FileSystem::cp(std::string const &source, std::string const &destination, bool recursive) -> void {
if (!does_exist(source)) throw std::invalid_argument("Source does not exist");
if (does_exist(destination)) throw std::invalid_argument("Destination already exists");

if (!recursive) {
shallow_copy(source, destination);
return;
}

auto source_cluster = search(source);
if (!source_cluster.has_value()) throw std::invalid_argument("Source does not exist");

auto source_meta = handler_builder_.build_metadata_handler(source_cluster.value()).read_metadata();
if (!source_meta.is_directory() || read_dir(source_cluster.value()).list_files().empty()) {
shallow_copy(source, destination);
return;
}

mkdir(destination);
auto destination_cluster = search(destination);
if (!destination_cluster.has_value()) throw std::invalid_argument("Destination does not exist");

auto source_dir = read_dir(source_cluster.value());
auto child_clusters = source_dir.list_files();
for (auto const &child_cluster : child_clusters) {
auto child_meta = handler_builder_.build_metadata_handler(child_cluster).read_metadata();
auto child_destination = path_resolver_.trace(destination_cluster.value()) + "/" + child_meta.get_name();
cp(path_resolver_.trace(child_cluster), child_destination, true);
}
deep_copy(source, destination);
}

auto FileSystem::mv(std::string const &source, std::string const &destination, bool recursive) -> void {
Expand Down Expand Up @@ -364,6 +342,8 @@ auto FileSystem::rmfile(std::string const &path) -> void {
}

auto FileSystem::shallow_copy(std::string const &source, std::string const &destination) -> void {
if (does_exist(destination)) throw std::invalid_argument("Destination already exists");

auto source_cluster = search(source);
if (!source_cluster.has_value()) throw std::invalid_argument("Source does not exist");

Expand Down Expand Up @@ -394,6 +374,32 @@ auto FileSystem::shallow_copy(std::string const &source, std::string const &dest
}
}

auto FileSystem::deep_copy(std::string const &source, std::string const &destination) -> void {
if (!does_exist(source)) throw std::invalid_argument("Source does not exist");
if (does_exist(destination)) throw std::invalid_argument("Destination already exists");

auto source_cluster = search(source);
if (!source_cluster.has_value()) throw std::invalid_argument("Source does not exist");

auto source_meta = handler_builder_.build_metadata_handler(source_cluster.value()).read_metadata();
if (!source_meta.is_directory() || read_dir(source_cluster.value()).list_files().empty()) {
shallow_copy(source, destination);
return;
}

mkdir(destination);
auto destination_cluster = search(destination);
if (!destination_cluster.has_value()) throw std::invalid_argument("Destination does not exist");

auto source_dir = read_dir(source_cluster.value());
auto child_clusters = source_dir.list_files();
for (auto const &child_cluster : child_clusters) {
auto child_meta = handler_builder_.build_metadata_handler(child_cluster).read_metadata();
auto child_destination = path_resolver_.trace(destination_cluster.value()) + "/" + child_meta.get_name();
cp(path_resolver_.trace(child_cluster), child_destination, true);
}
}

auto operator<<(std::ostream &out_stream, FileSystem const &file_system) -> std::ostream & {
out_stream << "FileSystem:\n";
out_stream << "Settings:\n";
Expand Down
1 change: 1 addition & 0 deletions src/FileSystem/FileSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ class FileSystem {
auto rmfile(std::string const &path) -> void;
auto rm_recursive(std::string const &path) -> void;
auto shallow_copy(std::string const &source, std::string const &destination) -> void;
auto deep_copy(std::string const &source, std::string const &destination) -> void;
};

0 comments on commit 5bdcdc6

Please sign in to comment.