|
| 1 | +// Copyright (c) Contributors to the SPK project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// https://github.com/spkenv/spk |
| 4 | + |
| 5 | +//! Definition and persistent storage of runtimes. |
| 6 | +
|
| 7 | +use std::fmt::Display; |
| 8 | +use std::path::PathBuf; |
| 9 | + |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | + |
| 12 | +use super::spec_api_version::SpecApiVersion; |
| 13 | +use crate::{Error, Result}; |
| 14 | + |
| 15 | +#[cfg(test)] |
| 16 | +#[path = "./live_layer_test.rs"] |
| 17 | +mod live_layer_test; |
| 18 | + |
| 19 | +/// Data needed to bind mount a path onto an /spfs backend that uses |
| 20 | +/// overlayfs. |
| 21 | +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] |
| 22 | +pub struct BindMount { |
| 23 | + /// Path to the source dir, or file, to bind mount into /spfs at |
| 24 | + /// the destination. |
| 25 | + #[serde(alias = "bind")] |
| 26 | + pub src: PathBuf, |
| 27 | + /// Where to attach the dir, or file, inside /spfs |
| 28 | + pub dest: String, |
| 29 | +} |
| 30 | + |
| 31 | +impl BindMount { |
| 32 | + /// Checks the bind mount is valid for use in /spfs with the given parent directory |
| 33 | + pub(crate) fn validate(&self, parent: PathBuf) -> Result<()> { |
| 34 | + if !self.src.starts_with(parent.clone()) { |
| 35 | + return Err(Error::String(format!( |
| 36 | + "Bind mount is not valid: {} is not under the live layer's directory: {}", |
| 37 | + self.src.display(), |
| 38 | + parent.display() |
| 39 | + ))); |
| 40 | + } |
| 41 | + |
| 42 | + if !self.src.exists() { |
| 43 | + return Err(Error::String(format!( |
| 44 | + "Bind mount is not valid: {} does not exist", |
| 45 | + self.src.display() |
| 46 | + ))); |
| 47 | + } |
| 48 | + |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl Display for BindMount { |
| 54 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 55 | + write!(f, "{}:{}", self.src.display(), self.dest) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// The kinds of contents that can be part of a live layer |
| 60 | +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] |
| 61 | +#[serde(untagged)] |
| 62 | +pub enum LiveLayerContents { |
| 63 | + /// A directory or file that will be bind mounted over /spfs |
| 64 | + BindMount(BindMount), |
| 65 | +} |
| 66 | + |
| 67 | +/// Data needed to add a live layer onto an /spfs overlayfs. |
| 68 | +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] |
| 69 | +pub struct LiveLayer { |
| 70 | + /// The api format version of the live layer data |
| 71 | + pub api: SpecApiVersion, |
| 72 | + /// The contents that the live layer will put into /spfs |
| 73 | + pub contents: Vec<LiveLayerContents>, |
| 74 | +} |
| 75 | + |
| 76 | +impl LiveLayer { |
| 77 | + /// Returns a list of the BindMounts in this LiveLayer |
| 78 | + pub fn bind_mounts(&self) -> Vec<BindMount> { |
| 79 | + self.contents |
| 80 | + .iter() |
| 81 | + .map(|c| match c { |
| 82 | + LiveLayerContents::BindMount(bm) => bm.clone(), |
| 83 | + }) |
| 84 | + .collect::<Vec<_>>() |
| 85 | + } |
| 86 | + |
| 87 | + /// Updates the live layer's contents entries using given parent |
| 88 | + /// directory. This will error if the resulting paths do not exist. |
| 89 | + /// |
| 90 | + /// This should be called before validate() |
| 91 | + fn set_parent(&mut self, parent: PathBuf) -> Result<()> { |
| 92 | + let mut new_contents = Vec::new(); |
| 93 | + |
| 94 | + for entry in self.contents.iter() { |
| 95 | + let new_entry = match entry { |
| 96 | + LiveLayerContents::BindMount(bm) => { |
| 97 | + let full_path = match parent.join(bm.src.clone()).canonicalize() { |
| 98 | + Ok(abs_path) => abs_path.clone(), |
| 99 | + Err(err) => { |
| 100 | + return Err(Error::InvalidPath(parent.join(bm.src.clone()), err)) |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + LiveLayerContents::BindMount(BindMount { |
| 105 | + src: full_path, |
| 106 | + dest: bm.dest.clone(), |
| 107 | + }) |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + new_contents.push(new_entry); |
| 112 | + } |
| 113 | + self.contents = new_contents; |
| 114 | + |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | + |
| 118 | + /// Validates the live layer's contents are under the given parent |
| 119 | + /// directory and accessible by the current user. |
| 120 | + /// |
| 121 | + /// This should be called after set_parent() |
| 122 | + fn validate(&self, parent: PathBuf) -> Result<()> { |
| 123 | + for entry in self.contents.iter() { |
| 124 | + match entry { |
| 125 | + LiveLayerContents::BindMount(bm) => bm.validate(parent.clone())?, |
| 126 | + } |
| 127 | + } |
| 128 | + Ok(()) |
| 129 | + } |
| 130 | + |
| 131 | + /// Sets the live layer's parent directory, which updates its |
| 132 | + /// contents, and then validates its contents. |
| 133 | + pub fn set_parent_and_validate(&mut self, parent: PathBuf) -> Result<()> { |
| 134 | + let abs_parent = match parent.canonicalize() { |
| 135 | + Ok(abs_path) => abs_path.clone(), |
| 136 | + Err(err) => return Err(Error::InvalidPath(parent.clone(), err)), |
| 137 | + }; |
| 138 | + |
| 139 | + self.set_parent(parent.clone())?; |
| 140 | + self.validate(abs_parent) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +impl Display for LiveLayer { |
| 145 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 146 | + write!(f, "{}:{:?}", self.api, self.contents) |
| 147 | + } |
| 148 | +} |
0 commit comments