Skip to content
Open
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
28 changes: 22 additions & 6 deletions crates/corrosion/src/command/tpl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{
collections::{HashMap, HashSet},
env::current_dir,
io::ErrorKind,
net::SocketAddr,
path::Path,
time::{Duration, Instant},
};

Expand Down Expand Up @@ -131,7 +133,10 @@ pub async fn run(

debug!("rendered template");

tokio::fs::rename(&tmp_filepath, &dst).await?;
if let Err(e) = atomic_rename_or_copy(&tmp_filepath, &dst).await {
error!("failed to write {}: {e}", dst);
break;
}

debug!("wrote file");

Expand Down Expand Up @@ -226,18 +231,29 @@ pub async fn run(
tokio::spawn(async_watch(filepaths));

while let Some(res) = futs.next().await {
match res {
Ok(_) => {
info!("")
}
Err(_) => todo!(),
if let Err(ref err) = res {
error!("template task failed: {err:#}");
}
println!("got a res: {res:?}");
}

Ok(())
}

async fn atomic_rename_or_copy(tmp: &Path, dst: &Utf8PathBuf) -> eyre::Result<()> {
match tokio::fs::rename(tmp, dst).await {
Ok(()) => Ok(()),
Err(e) if e.raw_os_error() == Some(18) => {
// libc::EXDEV cross link
info!("cross-device rename, falling back to copy");
tokio::fs::copy(tmp, dst).await?;
tokio::fs::remove_file(tmp).await?;
Ok(())
}
Err(e) => Err(e.into()),
}
}

fn async_watcher() -> notify::Result<(Debouncer<RecommendedWatcher>, Receiver<DebounceEventResult>)>
{
let (tx, rx) = channel(1);
Expand Down