Skip to content

Commit

Permalink
Refactor mkdir and add touch function
Browse files Browse the repository at this point in the history
This commit refactors the `mkdir` function in `FileSystem.cpp` to remove redundant code and adds a new `touch` function. The `touch` function creates a new file with the specified path if it doesn't already exist. These changes improve the overall functionality and maintainability of the codebase.
  • Loading branch information
GregoryKogan committed Dec 12, 2023
1 parent ae39ca8 commit aa2b71a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/FileSystem/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,26 @@ auto FileSystem::basename(std::string const &path) const -> std::string {

auto FileSystem::mkdir(std::string const &path) -> void {
if (does_file_exist(path)) throw std::invalid_argument("Already exists");
if (!does_dir_exist(dirname(path))) throw std::invalid_argument("Parent directory does not exist");

auto parent_dir_cluster = search(dirname(path));
if (!parent_dir_cluster.has_value()) throw std::invalid_argument("Parent directory does not exist");

auto new_dir_cluster = alloc_new_dir(basename(path), parent_dir_cluster.value());
add_file_to_dir(parent_dir_cluster.value(), new_dir_cluster);
}

auto FileSystem::touch(std::string const &path) -> void {
if (does_file_exist(path)) throw std::invalid_argument("Already exists");
auto parent_dir_cluster = search(dirname(path));
if (!parent_dir_cluster.has_value()) throw std::invalid_argument("Parent directory does not exist");

auto new_file_cluster = fat_.allocate();
auto new_file_byte_writer = handler_builder_.build_byte_writer(new_file_cluster);
auto new_file_meta = Metadata(basename(path), 0, new_file_cluster, parent_dir_cluster.value(), false);
new_file_byte_writer.write_bytes(0, new_file_meta.to_bytes());

add_file_to_dir(parent_dir_cluster.value(), new_file_cluster);
}

auto FileSystem::cd(std::string const &path) -> void {
auto dir_cluster = search(path);
if (!does_dir_exist(path) || !dir_cluster.has_value()) throw std::invalid_argument("Directory does not exist");
Expand Down
1 change: 1 addition & 0 deletions src/FileSystem/FileSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class FileSystem {
[[nodiscard]] auto dirname(std::string const &path) const -> std::string;
[[nodiscard]] auto basename(std::string const &path) const -> std::string;
auto mkdir(std::string const &path) -> void;
auto touch(std::string const &path) -> void;
auto cd(std::string const &path) -> void;

friend auto operator<<(std::ostream &out_stream, FileSystem const &file_system) -> std::ostream &;
Expand Down

0 comments on commit aa2b71a

Please sign in to comment.