Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented thiserror for libcontainer - Part 4 #1912

Merged
merged 10 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/libcontainer/src/container/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ impl Container {

pub fn save(&self) -> Result<()> {
tracing::debug!("Save container status: {:?} in {:?}", self, self.root);
self.state.save(&self.root)
self.state.save(&self.root)?;

Ok(())
}

pub fn spec(&self) -> Result<YoukiConfig> {
Expand Down
87 changes: 80 additions & 7 deletions crates/libcontainer/src/container/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::io::{BufReader, BufWriter, Write};
use std::path::PathBuf;
use std::{fs::File, path::Path};

use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use tracing::instrument;

/// Indicates status of the container
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -72,6 +72,27 @@ impl Display for ContainerStatus {
}
}

#[derive(Debug, thiserror::Error)]
pub enum StateError {
#[error("failed to open container state file {state_file_path:?}")]
OpenStateFile {
state_file_path: PathBuf,
source: std::io::Error,
},
#[error("failed to parse container state file {state_file_path:?}")]
ParseStateFile {
state_file_path: PathBuf,
source: serde_json::Error,
},
#[error("failed to write container state file {state_file_path:?}")]
WriteStateFile {
state_file_path: PathBuf,
source: std::io::Error,
},
}

type Result<T> = std::result::Result<T, StateError>;

/// Stores the state information of the container
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -125,6 +146,7 @@ impl State {
}
}

#[instrument(level = "trace")]
pub fn save(&self, container_root: &Path) -> Result<()> {
let state_file_path = Self::file_path(container_root);
let file = fs::OpenOptions::new()
Expand All @@ -134,19 +156,70 @@ impl State {
.create(true)
.truncate(true)
.open(&state_file_path)
.with_context(|| format!("failed to open {}", state_file_path.display()))?;
.map_err(|err| {
tracing::error!(
state_file_path = ?state_file_path,
err = %err,
"failed to open container state file",
);
StateError::OpenStateFile {
state_file_path: state_file_path.to_owned(),
source: err,
}
})?;
let mut writer = BufWriter::new(file);
serde_json::to_writer(&mut writer, self)?;
writer.flush()?;
serde_json::to_writer(&mut writer, self).map_err(|err| {
tracing::error!(
?state_file_path,
%err,
"failed to parse container state file",
);
StateError::ParseStateFile {
state_file_path: state_file_path.to_owned(),
source: err,
}
})?;
writer.flush().map_err(|err| {
tracing::error!(
?state_file_path,
%err,
"failed to write container state file",
);
StateError::WriteStateFile {
state_file_path: state_file_path.to_owned(),
source: err,
}
})?;

Ok(())
}

pub fn load(container_root: &Path) -> Result<Self> {
let state_file_path = Self::file_path(container_root);
let state_file = File::open(&state_file_path)
.with_context(|| format!("failed to open container state file {state_file_path:?}"))?;
let state_file = File::open(&state_file_path).map_err(|err| {
tracing::error!(
?state_file_path,
%err,
"failed to open container state file",
);
StateError::OpenStateFile {
state_file_path: state_file_path.to_owned(),
source: err,
}
})?;

let state: Self = serde_json::from_reader(BufReader::new(state_file)).map_err(|err| {
tracing::error!(
?state_file_path,
%err,
"failed to parse container state file",
);
StateError::ParseStateFile {
state_file_path: state_file_path.to_owned(),
source: err,
}
})?;

let state: Self = serde_json::from_reader(BufReader::new(state_file))?;
Ok(state)
}

Expand Down
10 changes: 10 additions & 0 deletions crates/libcontainer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ pub enum UnifiedSyscallError {
#[error(transparent)]
Syscall(#[from] crate::syscall::SyscallError),
}

#[derive(Debug, thiserror::Error)]
pub enum MissingSpecError {
#[error("missing process in spec")]
MissingProcess,
#[error("missing linux in spec")]
MissingLinux,
#[error("missing args in the process spec")]
MissingArgs,
}
Loading