68 lines
2.1 KiB
C++
68 lines
2.1 KiB
C++
#pragma once
|
|||
|
|
#include <sys/types.h>
|
||
|
|
|
||
|
|
#include <filesystem>
|
||
|
|
#include <optional>
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#include "network.hpp"
|
||
|
|
|
||
|
|
namespace doos {
|
||
|
|
|
||
|
|
class Container {
|
||
|
|
public:
|
||
|
|
Container(std::filesystem::path dir, std::string name) : dir_(std::move(dir)), name_(std::move(name)) {}
|
||
|
|
|
||
|
|
static void validateName(const std::string& name);
|
||
|
|
|
||
|
|
const std::string& name() const { return name_; }
|
||
|
|
const std::filesystem::path& dir() const { return dir_; }
|
||
|
|
std::filesystem::path rootfs() const { return dir_ / "rootfs"; }
|
||
|
|
std::filesystem::path layer(const std::string& hostDir) const; // layers/<name>/{upper,work}
|
||
|
|
std::filesystem::path privateDir(const std::string& hostDir) const;
|
||
|
|
std::filesystem::path lockFile() const { return dir_ / "lock"; }
|
||
|
|
std::filesystem::path pidFile() const { return dir_ / "pid"; }
|
||
|
|
std::filesystem::path metaFile() const { return dir_ / "meta"; }
|
||
|
|
|
||
|
|
NetMode net() const { return net_; }
|
||
|
|
long created() const { return created_; }
|
||
|
|
|
||
|
|
void create(NetMode net); // writes layout and meta
|
||
|
|
void load(); // reads meta
|
||
|
|
|
||
|
|
// Process id (outside view) of the container's init, if it is alive.
|
||
|
|
std::optional<pid_t> runningPid() const;
|
||
|
|
void recordPid(pid_t pid) const;
|
||
|
|
void clearPid() const;
|
||
|
|
|
||
|
|
// Creates the (empty) mountpoint skeleton and layer dirs. Idempotent.
|
||
|
|
void prepareLayout() const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::filesystem::path dir_;
|
||
|
|
std::string name_;
|
||
|
|
NetMode net_ = NetMode::Slirp;
|
||
|
|
long created_ = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
class Store {
|
||
|
|
public:
|
||
|
|
explicit Store(std::filesystem::path root);
|
||
|
|
static Store fromEnvironment();
|
||
|
|
|
||
|
|
std::vector<Container> list() const;
|
||
|
|
// Returns the container and whether it was created by this call.
|
||
|
|
std::pair<Container, bool> getOrCreate(const std::string& name, std::optional<NetMode> net);
|
||
|
|
// Dry-run unless confirmed. Returns a human-readable report line.
|
||
|
|
std::string remove(const std::string& name, bool confirmed);
|
||
|
|
|
||
|
|
const std::filesystem::path& root() const { return root_; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::filesystem::path containersDir() const { return root_ / "containers"; }
|
||
|
|
std::filesystem::path root_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace doos
|