use std::path::PathBuf;
use super::guest_volume::GuestVolumeManager;
#[derive(Debug, Clone)]
pub struct ContainerMount {
pub volume_name: String,
pub destination: String,
pub read_only: bool,
pub owner_uid: u32,
pub owner_gid: u32,
pub subpath: Option<String>,
}
pub struct ContainerVolumeManager<'a> {
guest: &'a mut GuestVolumeManager,
container_mounts: Vec<ContainerMount>,
}
impl<'a> ContainerVolumeManager<'a> {
pub fn new(guest: &'a mut GuestVolumeManager) -> Self {
Self {
guest,
container_mounts: Vec::new(),
}
}
#[allow(clippy::too_many_arguments)]
pub fn add_volume(
&mut self,
container_id: &str,
volume_name: &str,
tag: &str,
host_path: PathBuf,
container_path: &str,
read_only: bool,
owner_uid: u32,
owner_gid: u32,
subpath: Option<String>,
) {
self.guest.add_fs_share(
tag,
host_path,
None,
read_only,
Some(container_id.to_string()),
);
self.container_mounts.push(ContainerMount {
volume_name: volume_name.to_string(),
destination: container_path.to_string(),
read_only,
owner_uid,
owner_gid,
subpath,
});
}
#[allow(dead_code)]
pub fn add_bind(&mut self, volume_name: &str, container_path: &str, read_only: bool) {
self.container_mounts.push(ContainerMount {
volume_name: volume_name.to_string(),
destination: container_path.to_string(),
read_only,
owner_uid: 0,
owner_gid: 0,
subpath: None,
});
}
pub fn build_container_mounts(&self) -> Vec<ContainerMount> {
self.container_mounts.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_volume_carries_subpath_for_single_file() {
let mut guest = GuestVolumeManager::new();
let mut mgr = ContainerVolumeManager::new(&mut guest);
mgr.add_volume(
"cid",
"uservol0",
"uservol0",
PathBuf::from("/host/parent"),
"/etc/app.conf",
true,
0,
0,
Some("app.conf".to_string()),
);
let mounts = mgr.build_container_mounts();
assert_eq!(mounts.len(), 1);
assert_eq!(mounts[0].subpath, Some("app.conf".to_string()));
}
}