#![allow(clippy::derive_partial_eq_without_eq)]
use std::{collections::HashMap, fmt::Debug, ops::AddAssign, time::SystemTime};
use serde::{Deserialize, Serialize};
use tokio_stream::Stream;
pub use tonic;
mod convert;
mod wrap;
pub use wrap::server;
pub mod api {
pub mod snapshots {
pub mod v1 {
tonic::include_proto!("containerd.services.snapshots.v1");
}
}
pub mod types {
tonic::include_proto!("containerd.types");
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
pub enum Kind {
#[default]
Unknown,
View,
Active,
Committed,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Info {
pub kind: Kind,
pub name: String,
pub parent: String,
pub labels: HashMap<String, String>,
pub created_at: SystemTime,
pub updated_at: SystemTime,
}
impl Default for Info {
fn default() -> Self {
Info {
kind: Default::default(),
name: Default::default(),
parent: Default::default(),
labels: Default::default(),
created_at: SystemTime::now(),
updated_at: SystemTime::now(),
}
}
}
#[derive(Debug, Default)]
pub struct Usage {
pub inodes: i64,
pub size: i64,
}
impl AddAssign for Usage {
fn add_assign(&mut self, rhs: Self) {
self.inodes += rhs.inodes;
self.size += rhs.size;
}
}
#[tonic::async_trait]
pub trait Snapshotter: Send + Sync + 'static {
type Error: Debug + Into<tonic::Status> + Send;
async fn stat(&self, key: String) -> Result<Info, Self::Error>;
async fn update(
&self,
info: Info,
fieldpaths: Option<Vec<String>>,
) -> Result<Info, Self::Error>;
async fn usage(&self, key: String) -> Result<Usage, Self::Error>;
async fn mounts(&self, key: String) -> Result<Vec<api::types::Mount>, Self::Error>;
async fn prepare(
&self,
key: String,
parent: String,
labels: HashMap<String, String>,
) -> Result<Vec<api::types::Mount>, Self::Error>;
async fn view(
&self,
key: String,
parent: String,
labels: HashMap<String, String>,
) -> Result<Vec<api::types::Mount>, Self::Error>;
async fn commit(
&self,
name: String,
key: String,
labels: HashMap<String, String>,
) -> Result<(), Self::Error>;
async fn remove(&self, key: String) -> Result<(), Self::Error>;
async fn clear(&self) -> Result<(), Self::Error> {
Ok(())
}
type InfoStream: Stream<Item = Result<Info, Self::Error>> + Send + 'static;
async fn list(
&self,
snapshotter: String,
filters: Vec<String>,
) -> Result<Self::InfoStream, Self::Error>;
}