Skip to content

Commit

Permalink
Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
stuart6854 committed Dec 5, 2023
1 parent 5c36d4d commit 4b6dded
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion include/gfs/file_importer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ namespace gfs
public:
virtual ~FileImporter() = default;

virtual bool Import(Filesystem& fs, const std::filesystem::path& importFilename, MountID outputMount, const std::filesystem::path& outputDir) = 0;
virtual bool Import(Filesystem& fs,
const std::filesystem::path& importFilename,
MountID outputMount,
const std::filesystem::path& outputDir,
const std::string& metadata) = 0;
virtual bool Reimport(Filesystem& fs, const Filesystem::File& file) = 0;
};

Expand Down
6 changes: 4 additions & 2 deletions include/gfs/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace gfs
MountID MountId; // The Id of the mount this file is in.
std::filesystem::path MountRelPath; // Path to this file relative to the mount it is in.
std::filesystem::path SourceFilename; // The source file this file was imported from.
std::string MetadataStr; // String containg optional metadata eg. Import settings, etc.
std::vector<FileID> FileDependencies; // Files this file references.
uint32_t UncompressedSize;
uint32_t CompressedSize;
Expand Down Expand Up @@ -142,7 +143,8 @@ namespace gfs
const std::vector<FileID>& fileDependencies,
const BinaryStreamable& dataObject,
bool compress,
const std::filesystem::path& sourceFilename = "");
const std::filesystem::path& sourceFilename = "",
const std::string& metadata = "");

/**
* @brief
Expand Down Expand Up @@ -176,7 +178,7 @@ namespace gfs
*/
auto GetImporter(const std::string& fileExt) -> std::shared_ptr<FileImporter>;

bool Import(const std::filesystem::path& filename, MountID outputMount, const std::filesystem::path& outputDir);
bool Import(const std::filesystem::path& filename, MountID outputMount, const std::filesystem::path& outputDir, const std::string& metadata = "");

bool Reimport(FileID fileId);

Expand Down
22 changes: 18 additions & 4 deletions src/gfs/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ namespace gfs
const std::vector<FileID>& fileDependencies,
const BinaryStreamable& dataObject,
bool compress,
const std::filesystem::path& sourceFilename)
const std::filesystem::path& sourceFilename,
const std::string& metadata)
{
auto* mount = GetMount(mountId);
if (!mount)
Expand Down Expand Up @@ -142,6 +143,7 @@ namespace gfs
file.MountId = mountId;
file.MountRelPath = filename;
file.SourceFilename = sourceFilename;
file.MetadataStr = metadata;
file.FileDependencies = fileDependencies;
file.UncompressedSize = uint32_t(uncompressedDataBuffer.GetSize());
file.CompressedSize = uint32_t(compressedDataBuffer.GetSize());
Expand Down Expand Up @@ -312,17 +314,17 @@ namespace gfs
return it->second;
}

bool Filesystem::Import(const std::filesystem::path& filename, MountID outputMount, const std::filesystem::path& outputDir)
bool Filesystem::Import(const std::filesystem::path& filename, MountID outputMount, const std::filesystem::path& outputDir, const std::string& metadata)
{
if (filename.empty() || !std::filesystem::exists(filename) || !std::filesystem::is_regular_file(filename))
return false;

const auto fileExt = filename.extension().string();
auto importer = GetImporter(fileExt);
const auto importer = GetImporter(fileExt);
if (!importer)
return false;

return importer->Import(*this, filename, outputMount, outputDir);
return importer->Import(*this, filename, outputMount, outputDir, metadata);
}

bool Filesystem::Reimport(FileID fileId)
Expand Down Expand Up @@ -523,6 +525,11 @@ namespace gfs
pathStr = file.SourceFilename.string();
stream.write(reinterpret_cast<const char*>(pathStr.data()), strLen);

// Metadata
strLen = file.MetadataStr.size();
stream.write(reinterpret_cast<const char*>(&strLen), sizeof(strLen));
stream.write(reinterpret_cast<const char*>(file.MetadataStr.data()), strLen);

// File dependencies
const auto count = uint16_t(file.FileDependencies.size());
stream.write(reinterpret_cast<const char*>(&count), sizeof(count));
Expand Down Expand Up @@ -553,6 +560,13 @@ namespace gfs
stream.read(reinterpret_cast<char*>(pathStr.data()), strLen);
file.SourceFilename = pathStr;

// Source filename
strLen = 0;
stream.read(reinterpret_cast<char*>(&strLen), sizeof(strLen));
pathStr = std::string(strLen, 0);
stream.read(reinterpret_cast<char*>(pathStr.data()), strLen);
file.MetadataStr = pathStr;

// File dependencies
uint16_t count = 0;
stream.read(reinterpret_cast<char*>(&count), sizeof(count));
Expand Down
6 changes: 5 additions & 1 deletion testbed/testbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ struct TextResource : gfs::BinaryStreamable

struct TextFileImporter : gfs::FileImporter
{
bool Import(gfs::Filesystem& fs, const std::filesystem::path& importFilename, gfs::MountID outputMount, const std::filesystem::path& outputDir) override
bool Import(gfs::Filesystem& fs,
const std::filesystem::path& importFilename,
gfs::MountID outputMount,
const std::filesystem::path& outputDir,
const std::string& metadata) override
{
auto text = ReadTextFile(importFilename);
if (text.empty())
Expand Down

0 comments on commit 4b6dded

Please sign in to comment.