Skip to content

Commit

Permalink
fix unwraps, use a ref to sender and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Sep 18, 2024
1 parent 225fb8a commit 8b966ca
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions docker2fl/src/docker2fl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl DockerImageToFlist {
Ok(())
}

pub async fn pack<S: Store>(&mut self, store: S, sender: Option<Sender<i32>>) -> Result<()> {
pub async fn pack<S: Store>(&mut self, store: S, sender: Option<Sender<u32>>) -> Result<()> {
rfs::pack(
self.meta.clone(),
store,
Expand All @@ -121,7 +121,7 @@ impl DockerImageToFlist {
Ok(())
}

pub async fn convert<S: Store>(&mut self, store: S, sender: Option<Sender<i32>>) -> Result<()> {
pub async fn convert<S: Store>(&mut self, store: S, sender: Option<Sender<u32>>) -> Result<()> {
self.prepare().await?;
self.pack(store, sender).await?;

Expand Down
17 changes: 11 additions & 6 deletions fl-server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ pub async fn create_flist_handler(
);

let container_name = Uuid::new_v4().to_string();
let docker_tmp_dir = tempdir::TempDir::new(&container_name).unwrap();
let docker_tmp_dir =
tempdir::TempDir::new(&container_name).expect("failed to create tmp dir for docker");
let docker_tmp_dir_path = docker_tmp_dir.path().to_owned();

let (tx, rx) = mpsc::channel();
Expand All @@ -193,7 +194,7 @@ pub async fn create_flist_handler(
state
.jobs_state
.lock()
.unwrap()
.expect("failed to lock state")
.insert(job.id.clone(), FlistState::Failed);
return;
}
Expand All @@ -206,10 +207,10 @@ pub async fn create_flist_handler(
let mut progress: f32 = 0.0;

for _ in 0..files_count - 1 {
let step = rx.recv().unwrap() as f32;
let step = rx.recv().expect("failed to receive progress") as f32;
progress += step;
let progress_percentage = progress / files_count as f32 * 100.0;
st.jobs_state.lock().unwrap().insert(
st.jobs_state.lock().expect("failed to lock state").insert(
job_id.clone(),
FlistState::InProgress(FlistStateInfo {
msg: "flist is in progress".to_string(),
Expand All @@ -218,7 +219,7 @@ pub async fn create_flist_handler(
);
st.flists_progress
.lock()
.unwrap()
.expect("failed to lock state")
.insert(cloned_fl_path.clone(), progress_percentage);
}
});
Expand Down Expand Up @@ -248,7 +249,11 @@ pub async fn create_flist_handler(
flist_download_url
)),
);
state.flists_progress.lock().unwrap().insert(fl_path, 100.0);
state
.flists_progress
.lock()
.expect("failed to lock state")
.insert(fl_path, 100.0);
});

Ok(ResponseResult::FlistCreated(current_job))
Expand Down
4 changes: 2 additions & 2 deletions fl-server/src/serve_flists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub async fn visit_dir_one_level<P: AsRef<std::path::Path>>(
match state
.flists_progress
.lock()
.unwrap()
.expect("failed to lock state")
.get(&path.join(&name).to_path_buf())
{
Some(p) => progress = *p,
Expand All @@ -113,7 +113,7 @@ pub async fn visit_dir_one_level<P: AsRef<std::path::Path>>(
let ext = child
.path()
.extension()
.unwrap()
.expect("failed to get path extension")
.to_string_lossy()
.to_string();
if ext != "fl" {
Expand Down
16 changes: 6 additions & 10 deletions rfs/src/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn pack<P: Into<PathBuf>, S: Store>(
store: S,
root: P,
strip_password: bool,
sender: Option<Sender<i32>>,
sender: Option<Sender<u32>>,
) -> Result<()> {
use tokio::fs;

Expand Down Expand Up @@ -72,13 +72,13 @@ pub async fn pack<P: Into<PathBuf>, S: Store>(
&writer,
&mut pool,
Item(0, root, OsString::from("/"), meta),
sender.clone(),
sender.as_ref(),
)
.await?;

while !list.is_empty() {
let dir = list.pop_back().unwrap();
pack_one(&mut list, &writer, &mut pool, dir, sender.clone()).await?;
pack_one(&mut list, &writer, &mut pool, dir, sender.as_ref()).await?;
}

pool.close().await;
Expand All @@ -105,7 +105,7 @@ async fn pack_one<S: Store>(
writer: &Writer,
pool: &mut WorkerPool<Uploader<S>>,
Item(parent, path, name, meta): Item,
sender: Option<Sender<i32>>,
sender: Option<&Sender<u32>>,
) -> Result<()> {
use std::os::unix::fs::MetadataExt;
use tokio::fs;
Expand Down Expand Up @@ -139,12 +139,8 @@ async fn pack_one<S: Store>(
let meta = child.metadata().await?;
let child_path = path.join(&name);

if sender.is_some() {
sender
.clone()
.unwrap()
.send(1)
.context("failed to send progress")?;
if let Some(ref sender) = sender {
sender.send(1).context("failed to send progress")?;
}

// if this child a directory we add to the tail of the list
Expand Down

0 comments on commit 8b966ca

Please sign in to comment.