From 699d610632389a0320eaa98b128d4cc158571637 Mon Sep 17 00:00:00 2001 From: Kevin Wheatley Date: Tue, 16 Jan 2024 21:06:19 +0000 Subject: [PATCH] Add Basic minimal archive functionallity Needs documenting and testing May be nice to have a progress report and/or integration with OCIO Logging Signed-off-by: Kevin Wheatley --- include/OpenColorIO/OpenColorIO.h | 4 ++++ src/OpenColorIO/Config.cpp | 5 +++++ src/OpenColorIO/OCIOZArchive.cpp | 30 +++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/OpenColorIO/OpenColorIO.h b/include/OpenColorIO/OpenColorIO.h index f091068ffc..f626c26cc8 100644 --- a/include/OpenColorIO/OpenColorIO.h +++ b/include/OpenColorIO/OpenColorIO.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "OpenColorABI.h" #include "OpenColorTypes.h" @@ -1534,6 +1535,9 @@ class OCIOEXPORT Config */ void archive(std::ostream & ostream, ArchiveFlags flags) const; + //TODO: document + void GetAllFileReferences(std::set & files) const; + Config(const Config &) = delete; Config& operator= (const Config &) = delete; diff --git a/src/OpenColorIO/Config.cpp b/src/OpenColorIO/Config.cpp index d52029936d..578c8dd054 100644 --- a/src/OpenColorIO/Config.cpp +++ b/src/OpenColorIO/Config.cpp @@ -5547,4 +5547,9 @@ void Config::archive(std::ostream & ostream, ArchiveFlags flags) const archiveConfig(ostream, *this, getCurrentContext()->getWorkingDir(), flags); } +void Config::GetAllFileReferences(std::set & files) const +{ + return getImpl()->GetAllFileReferences(files); +} + } // namespace OCIO_NAMESPACE diff --git a/src/OpenColorIO/OCIOZArchive.cpp b/src/OpenColorIO/OCIOZArchive.cpp index e9f5f756eb..8d6c0b544e 100644 --- a/src/OpenColorIO/OCIOZArchive.cpp +++ b/src/OpenColorIO/OCIOZArchive.cpp @@ -200,6 +200,34 @@ void addSupportedFiles(void * archiver, const char * path, const char * configWo mz_os_close_dir(dir); } } + +void addReferencedFiles(void * archiver, const Config & config) +{ + ConstContextRcPtr context = config.getCurrentContext(); + ContextRcPtr ctxFilepath = Context::Create(); + ctxFilepath->setSearchPath(context->getSearchPath()); + ctxFilepath->setWorkingDir(context->getWorkingDir()); + ctxFilepath->setConfigIOProxy(context->getConfigIOProxy()); + + auto prefixLength = std::string(context->getWorkingDir()).length() + 1; // +1 add trailing '/' TODO: improve this + + std::set files; + config.GetAllFileReferences(files); + for (const auto &file : files) + { + const std::string resolvedPath = context->resolveFileLocation(file.c_str(), ctxFilepath); + const std::string relativePath = resolvedPath.substr(prefixLength); + + auto returnCode = mz_zip_writer_add_file(archiver, resolvedPath.c_str(), relativePath.c_str()); + if (returnCode != MZ_OK) + { + std::ostringstream os; + os << "Could not write file " << resolvedPath << " to in-memory archive.()" << returnCode << ")"; + throw Exception(os.str().c_str()); + } + } +} + ////////////////////////////////////////////////////////////////////////////////////// ArchiveFlags EnvironmentOverride(ArchiveFlags oFlags) // TODO: test override @@ -321,7 +349,7 @@ void archiveConfig(std::ostream & ostream, const Config & config, const char * c // Add all supported files to in-memory zip from any directories under working directory. // (recursive) if (HasFlag(flags, ARCHIVE_FLAGS_MINIMAL)) - addSupportedFiles(archiver, configWorkingDirectory, configWorkingDirectory); + addReferencedFiles(archiver, config); else addSupportedFiles(archiver, configWorkingDirectory, configWorkingDirectory);