Skip to content

Commit

Permalink
Add Basic minimal archive functionallity
Browse files Browse the repository at this point in the history
Needs documenting and testing
May be nice to have a progress report and/or integration with OCIO Logging

Signed-off-by: Kevin Wheatley <[email protected]>
  • Loading branch information
KevinJW committed Jan 22, 2024
1 parent a115736 commit 54121ce
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/OpenColorIO/OpenColorIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <fstream>
#include <vector>
#include <set>

#include "OpenColorABI.h"
#include "OpenColorTypes.h"
Expand Down Expand Up @@ -1534,6 +1535,9 @@ class OCIOEXPORT Config
*/
void archive(std::ostream & ostream, ArchiveFlags flags) const;

//TODO: document
void GetAllFileReferences(std::set<std::string> & files) const;

Config(const Config &) = delete;
Config& operator= (const Config &) = delete;

Expand Down
5 changes: 5 additions & 0 deletions src/OpenColorIO/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5547,4 +5547,9 @@ void Config::archive(std::ostream & ostream, ArchiveFlags flags) const
archiveConfig(ostream, *this, getCurrentContext()->getWorkingDir(), flags);
}

void Config::GetAllFileReferences(std::set<std::string> & files) const
{
return getImpl()->GetAllFileReferences(files);
}

} // namespace OCIO_NAMESPACE
30 changes: 29 additions & 1 deletion src/OpenColorIO/OCIOZArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 54121ce

Please sign in to comment.