From b00704ef37155c17daff1986cda3274d95560bad Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 11 Sep 2022 00:45:57 +0200 Subject: Extended dat update action with recursive behaviour --- ArchiveEx/dat.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/ArchiveEx/dat.cpp b/ArchiveEx/dat.cpp index 06beba1..a8f112d 100644 --- a/ArchiveEx/dat.cpp +++ b/ArchiveEx/dat.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -9,6 +10,9 @@ #include +namespace fs = std::filesystem; + + enum class Action { NOTHING, @@ -41,6 +45,7 @@ public: static Options ParseArgs(int argc, char* argv[]); static void PrintUsage(std::ostream& out=std::cerr); static void PrintHelp(std::ostream& out=std::cout); +static void UpdateFile(ArchiveEx::Archive& archive, const std::string& path, bool warn=true); static const char* program {"dat"}; @@ -69,7 +74,7 @@ try { break; case Action::UPDATE: for (const auto& file : opts.files) - archive.Insert(file.c_str()); // Support recursive directories tar-like + UpdateFile(archive, file); break; case Action::EXTRACT: break; // Not implemented @@ -147,3 +152,23 @@ PrintHelp(std::ostream& out) out << " -x\tExtracts files from the archive." << std::endl; out << " -c\tCreate the archive if it does not exist." << std::endl; } + + +void +UpdateFile(ArchiveEx::Archive& archive, const std::string& path, bool warn) +{ + const auto status = fs::status(path); + switch (status.type()) { + case fs::file_type::regular: + archive.Insert(path.c_str()); + break; + case fs::file_type::directory: + for (const auto& entry : fs::directory_iterator{path}) + UpdateFile(archive, entry.path(), false); + break; + default: + if (warn) + std::cerr << "unsupported file type: " << path << std::endl; + break; + } +} -- cgit v1.1