From d0e356d09e30a11c1e072415a5088f829d5c0a04 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 16 Jan 2022 21:32:01 -0500 Subject: Worked on features --- src/cmd/cmd_fsops.cc | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/cmd/cmd_fsops.cc (limited to 'src/cmd/cmd_fsops.cc') diff --git a/src/cmd/cmd_fsops.cc b/src/cmd/cmd_fsops.cc new file mode 100644 index 0000000..e638b96 --- /dev/null +++ b/src/cmd/cmd_fsops.cc @@ -0,0 +1,87 @@ +#include "cmd.h" +#include "../utils.h" +#include "../entry.h" +#include +#include + +namespace fs = std::filesystem; + +namespace cmd { + std::string list(const fs::path& p) { + std::stringstream text; + if(p.empty()) { + // Print read-only dirs + for(std::string name : getVirtDirs()) { + text << name << "/ (read only)" << std::endl; + } + } + fs::path truePath = getTruePath(p); + if(fs::directory_entry(truePath).is_regular_file()) { + text << utils::instantiate(truePath)->getText() << std::endl; + } + else if(fs::directory_entry(truePath).is_directory()) { + for(fs::directory_entry de : fs::directory_iterator(truePath)) { + if(de.path().filename().string()[0] != '.') { + if(de.is_directory()) { + text << de.path().filename().string() << "/" << std::endl; + } else { + text << de.path().stem().string() << std::endl; + } + } + } + } + else { + text << "Unknown path " << p << std::endl; + } + return text.str(); + } + + std::string list(std::vector args) { + std::stringstream text; + if(args.empty()) { + text << list(""); + } else { + for(std::string dir : args) { + text << list(dir); + } + } + return text.str(); + } + + std::string mkdir(std::vector args) { + for(std::string s : args) { + fs::create_directories(getTruePath(s)); + } + return ""; + } + + void cp(fs::path src, fs::path dest) { + if(fs::directory_entry(src).is_regular_file()) { + utils::saveJson(utils::instantiate(src)->serialize(), dest); + } else { + mkdir({dest}); + for(fs::directory_entry de : fs::directory_iterator(src)) { + cp(de.path(), dest / de.path().filename()); + } + } + } + + std::string cp(std::vector args) { + // Operate by intantiating and saving + // We do recursive! + cp(getTruePath(args[0]), getTruePath(args[1])); + return ""; + } + + std::string mv(std::vector args) { + fs::rename(getTruePath(args[0]), getTruePath(args[1])); + return ""; + } + + std::string rm(std::vector args) { + for(std::string s : args) { + fs::remove_all(getTruePath(s)); + } + return ""; + } +} -- cgit v1.2.3