Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
60 lines
1.7 KiB
C++
60 lines
1.7 KiB
C++
// Spec: plans/01-runtime.md 6.7. Asset provider interface: DiskProvider
|
|
// (root jailed) and EmbeddedProvider (from the 09-cli-ops.md embedder) share
|
|
// one FileServer logic.
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace molo::runtime::static_ {
|
|
|
|
struct AssetMeta {
|
|
uint64_t length = 0;
|
|
int64_t mtime = 0; // unix seconds
|
|
bool is_file = false;
|
|
bool is_dir = false;
|
|
};
|
|
|
|
// Move-only open asset. Disk assets carry an fd for sendfile; embedded
|
|
// assets carry a memory view that stays valid while the provider lives.
|
|
class Asset {
|
|
public:
|
|
Asset() = default;
|
|
~Asset();
|
|
Asset(Asset&& other) noexcept;
|
|
Asset& operator=(Asset&& other) noexcept;
|
|
Asset(const Asset&) = delete;
|
|
Asset& operator=(const Asset&) = delete;
|
|
|
|
bool valid() const { return valid_; }
|
|
AssetMeta meta() const { return meta_; }
|
|
int fd() const { return fd_; } // -1 for memory assets
|
|
const char* data() const { return data_; }
|
|
std::size_t size() const { return size_; }
|
|
// Hands the fd to the caller (for FileBody); this asset becomes invalid.
|
|
int release();
|
|
|
|
static Asset ForFile(int fd, AssetMeta meta);
|
|
static Asset ForMemory(const char* data, std::size_t size, AssetMeta meta);
|
|
static Asset ForDir(AssetMeta meta);
|
|
|
|
private:
|
|
bool valid_ = false;
|
|
AssetMeta meta_;
|
|
int fd_ = -1;
|
|
const char* data_ = nullptr;
|
|
std::size_t size_ = 0;
|
|
};
|
|
|
|
class IAssetProvider {
|
|
public:
|
|
virtual ~IAssetProvider() = default;
|
|
// rel is a guard-normalized relative path (see path_guard.h).
|
|
virtual std::optional<Asset> open(std::string_view rel) = 0;
|
|
virtual const char* name() const = 0;
|
|
};
|
|
|
|
} // namespace molo::runtime::static_
|