Skip to content

Commit

Permalink
use crate::common::fs instead of tokio::fs
Browse files Browse the repository at this point in the history
Convert tokio::fs call sites to use crate::common::fs for better error
messages. Also fix a case of blocking io.
  • Loading branch information
webern committed Dec 26, 2023
1 parent 85544b3 commit f6b4037
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
8 changes: 3 additions & 5 deletions twoliter/src/cmd/debug.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::common::fs;
use crate::tools::install_tools;
use anyhow::{Context, Result};
use anyhow::Result;
use clap::Parser;
use std::env;
use std::path::PathBuf;
use tokio::fs;
use uuid::Uuid;

#[derive(Debug, Clone, Parser)]
Expand Down Expand Up @@ -49,9 +49,7 @@ impl CheckToolArgs {
.install_dir
.clone()
.unwrap_or_else(|| env::temp_dir().join(unique_name()));
fs::create_dir_all(&dir)
.await
.context(format!("Unable to create directory '{}'", dir.display()))?;
fs::create_dir_all(&dir).await?;
install_tools(&dir).await?;
println!("{}", dir.display());
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion twoliter/src/cmd/make.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::cargo_make::CargoMake;
use crate::common::fs;
use crate::project::{self};
use crate::tools::install_tools;
use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;
use tokio::fs;

/// Run a cargo make command in Twoliter's build environment. Known Makefile.toml environment
/// variables will be passed-through to the cargo make invocation.
Expand Down
28 changes: 6 additions & 22 deletions twoliter/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::common::fs;
use crate::docker::ImageArchUri;
use crate::schema_version::SchemaVersion;
use anyhow::{ensure, Context, Result};
Expand All @@ -7,7 +8,6 @@ use non_empty_string::NonEmptyString;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha512};
use std::path::{Path, PathBuf};
use tokio::fs;
use toml::Table;

/// Common functionality in commands, if the user gave a path to the `Twoliter.toml` file,
Expand Down Expand Up @@ -241,9 +241,9 @@ impl UnvalidatedProject {
#[cfg(test)]
mod test {
use super::*;
use crate::common::fs;
use crate::test::data_dir;
use tempfile::TempDir;
use tokio::fs;

/// Ensure that `Twoliter.toml` can be deserialized.
#[tokio::test]
Expand Down Expand Up @@ -341,18 +341,10 @@ mod test {
let release_toml_to = p.join("Release.toml");
fs::copy(&twoliter_toml_from, &twoliter_toml_to)
.await
.expect(&format!(
"Unable to copy {} to {}",
twoliter_toml_from.display(),
twoliter_toml_to.display()
));
.unwrap();
fs::copy(&release_toml_from, &release_toml_to)
.await
.expect(&format!(
"Unable to copy {} to {}",
release_toml_from.display(),
release_toml_to.display()
));
.unwrap();
let result = Project::find_and_load(&p).await;
assert!(
result.is_err(),
Expand All @@ -372,18 +364,10 @@ mod test {
let release_toml_to = p.join("Release.toml");
fs::copy(&twoliter_toml_from, &twoliter_toml_to)
.await
.expect(&format!(
"Unable to copy {} to {}",
twoliter_toml_from.display(),
twoliter_toml_to.display()
));
.unwrap();
fs::copy(&release_toml_from, &release_toml_to)
.await
.expect(&format!(
"Unable to copy {} to {}",
release_toml_from.display(),
release_toml_to.display()
));
.unwrap();

// The project should load because Release.toml and Twoliter.toml versions match.
Project::find_and_load(&p).await.unwrap();
Expand Down
18 changes: 13 additions & 5 deletions twoliter/src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::common::fs;
use anyhow::{Context, Result};
use filetime::{set_file_handle_times, set_file_mtime, FileTime};
use flate2::read::ZlibDecoder;
use log::debug;
use std::path::Path;
use tar::Archive;
use tokio::fs;
use tokio::fs::OpenOptions;
use tokio::io::AsyncWriteExt;
use tokio::runtime::Handle;

const TAR_GZ_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/tools.tar.gz"));
const BOTTLEROCKET_VARIANT: &[u8] =
Expand Down Expand Up @@ -49,7 +51,7 @@ pub(crate) async fn install_tools(tools_dir: impl AsRef<Path>) -> Result<()> {

async fn write_bin(name: &str, data: &[u8], dir: impl AsRef<Path>, mtime: FileTime) -> Result<()> {
let path = dir.as_ref().join(name);
let mut f = fs::OpenOptions::new()
let mut f = OpenOptions::new()
.create(true)
.read(false)
.write(true)
Expand All @@ -65,9 +67,15 @@ async fn write_bin(name: &str, data: &[u8], dir: impl AsRef<Path>, mtime: FileTi
.context(format!("Unable to finalize '{}'", path.display()))?;

let f = f.into_std().await;
set_file_handle_times(&f, None, Some(mtime))
.context(format!("Unable to set mtime for '{}'", path.display()))?;
Ok(())
let rt = Handle::current();
rt.spawn_blocking(move || {
set_file_handle_times(&f, None, Some(mtime))
.context(format!("Unable to set mtime for '{}'", path.display()))
})
.await
.context(format!(
"Unable to run and join async task for reading handle time"
))?
}

async fn unpack_tarball(tools_dir: impl AsRef<Path>) -> Result<()> {
Expand Down

0 comments on commit f6b4037

Please sign in to comment.