#![allow(missing_docs)]
#[allow(unused_imports)]
use crate::builder::OnVm;
#[allow(unused_imports)]
use crate::ids::GuestPath;
#[allow(unused_imports)]
use firkin_oci::ImageBundle;
#[allow(unused_imports)]
use firkin_types::BlockDeviceId;
#[allow(unused_imports)]
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Rootfs {
Ext4Image(PathBuf),
OciBundle(Box<ImageBundle>),
RawBlock(PathBuf),
}
impl Rootfs {
#[must_use]
pub fn ext4_image(path: impl Into<PathBuf>) -> Self {
Self::Ext4Image(path.into())
}
#[must_use]
pub fn oci_bundle(bundle: ImageBundle) -> Self {
Self::OciBundle(Box::new(bundle))
}
#[must_use]
pub fn raw_block(path: impl Into<PathBuf>) -> Self {
Self::RawBlock(path.into())
}
#[must_use]
pub const fn block_device(id: BlockDeviceId) -> VmRootfs {
VmRootfs::BlockDevice(id)
}
}
impl From<PathBuf> for Rootfs {
fn from(value: PathBuf) -> Self {
Self::Ext4Image(value)
}
}
impl From<&Path> for Rootfs {
fn from(value: &Path) -> Self {
Self::Ext4Image(value.to_path_buf())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum VmRootfs {
BlockDevice(BlockDeviceId),
GuestPath(GuestPath),
}
impl VmRootfs {
#[must_use]
pub fn guest_path(path: GuestPath) -> Self {
Self::GuestPath(path)
}
#[must_use]
pub const fn as_block_device(&self) -> Option<BlockDeviceId> {
match self {
Self::BlockDevice(id) => Some(*id),
Self::GuestPath(_) => None,
}
}
#[must_use]
pub fn as_guest_path(&self) -> Option<&GuestPath> {
match self {
Self::BlockDevice(_) => None,
Self::GuestPath(path) => Some(path),
}
}
}
impl From<BlockDeviceId> for VmRootfs {
fn from(value: BlockDeviceId) -> Self {
Self::BlockDevice(value)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(dead_code)]
pub(crate) struct StagedRootfs {
pub(crate) path: PathBuf,
}
#[allow(dead_code)]
impl StagedRootfs {
pub(crate) fn new(path: impl Into<PathBuf>) -> Self {
Self { path: path.into() }
}
pub(crate) fn path(&self) -> &Path {
&self.path
}
}
#[derive(Debug)]
pub(crate) enum CopyInPayload {
File { path: PathBuf, mode: u32 },
DirectoryArchive { archive: tempfile::NamedTempFile },
}
impl CopyInPayload {
pub(crate) fn path(&self) -> &Path {
match self {
Self::File { path, .. } => path,
Self::DirectoryArchive { archive } => archive.path(),
}
}
pub(crate) const fn mode(&self) -> u32 {
match self {
Self::File { mode, .. } => *mode,
Self::DirectoryArchive { .. } => 0,
}
}
pub(crate) const fn is_archive(&self) -> bool {
matches!(self, Self::DirectoryArchive { .. })
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum RootfsChoice {
Implicit(Box<Rootfs>),
OnVm(VmRootfs),
}