mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 10:21:52 +00:00
* store, properties, manifest * interfaces * init and backups * fix init and backups * file models * more versions * dependencies * config except dynamic types * clean up config * remove disabled from non-dynamic vaues * actions * standardize example code block formats * wip: actions refactor Co-authored-by: Jade <Blu-J@users.noreply.github.com> * commit types * fix types * update types * update action request type * update apis * add description to actionrequest * clean up imports * revert package json * chore: Remove the recursive to the index * chore: Remove the other thing I was testing * flatten action requests * update container runtime with new config paradigm * new actions strategy * seems to be working * misc backend fixes * fix fe bugs * only show breakages if breakages * only show success modal if result * don't panic on failed removal * hide config from actions page * polyfill autoconfig * use metadata strategy for actions instead of prev * misc fixes * chore: split the sdk into 2 libs (#2736) * follow sideload progress (#2718) * follow sideload progress * small bugfix * shareReplay with no refcount false * don't wrap sideload progress in RPCResult * dont present toast --------- Co-authored-by: Aiden McClelland <me@drbonez.dev> * chore: Add the initial of the creation of the two sdk * chore: Add in the baseDist * chore: Add in the baseDist * chore: Get the web and the runtime-container running * chore: Remove the empty file * chore: Fix it so the container-runtime works --------- Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com> Co-authored-by: Aiden McClelland <me@drbonez.dev> * misc fixes * update todos * minor clean up * fix link script * update node version in CI test * fix node version syntax in ci build * wip: fixing callbacks * fix sdk makefile dependencies * add support for const outside of main * update apis * don't panic! * Chore: Capture weird case on rpc, and log that * fix procedure id issue * pass input value for dep auto config * handle disabled and warning for actions * chore: Fix for link not having node_modules * sdk fixes * fix build * fix build * fix build --------- Co-authored-by: Matt Hill <mattnine@protonmail.com> Co-authored-by: Jade <Blu-J@users.noreply.github.com> Co-authored-by: J H <dragondef@gmail.com> Co-authored-by: Jade <2364004+Blu-J@users.noreply.github.com> Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com>
263 lines
7.3 KiB
Rust
263 lines
7.3 KiB
Rust
use std::future::Future;
|
|
use std::ops::{Deref, DerefMut};
|
|
use std::path::{Path, PathBuf};
|
|
use std::time::Duration;
|
|
|
|
use color_eyre::eyre::{eyre, Context, Error};
|
|
use futures::future::BoxFuture;
|
|
use futures::FutureExt;
|
|
use models::ResultExt;
|
|
use tokio::fs::File;
|
|
use tokio::sync::oneshot;
|
|
use tokio::task::{JoinError, JoinHandle, LocalSet};
|
|
|
|
mod byte_replacement_reader;
|
|
mod rsync;
|
|
mod script_dir;
|
|
pub use byte_replacement_reader::*;
|
|
pub use rsync::*;
|
|
pub use script_dir::*;
|
|
|
|
pub fn const_true() -> bool {
|
|
true
|
|
}
|
|
|
|
pub fn to_tmp_path(path: impl AsRef<Path>) -> Result<PathBuf, Error> {
|
|
let path = path.as_ref();
|
|
if let (Some(parent), Some(file_name)) =
|
|
(path.parent(), path.file_name().and_then(|f| f.to_str()))
|
|
{
|
|
Ok(parent.join(format!(".{}.tmp", file_name)))
|
|
} else {
|
|
Err(eyre!("invalid path: {}", path.display()))
|
|
}
|
|
}
|
|
|
|
pub async fn canonicalize(
|
|
path: impl AsRef<Path> + Send + Sync,
|
|
create_parent: bool,
|
|
) -> Result<PathBuf, Error> {
|
|
fn create_canonical_folder<'a>(
|
|
path: impl AsRef<Path> + Send + Sync + 'a,
|
|
) -> BoxFuture<'a, Result<PathBuf, Error>> {
|
|
async move {
|
|
let path = canonicalize(path, true).await?;
|
|
tokio::fs::create_dir(&path)
|
|
.await
|
|
.with_context(|| path.display().to_string())?;
|
|
Ok(path)
|
|
}
|
|
.boxed()
|
|
}
|
|
let path = path.as_ref();
|
|
if tokio::fs::metadata(path).await.is_err() {
|
|
let parent = path.parent().unwrap_or(Path::new("."));
|
|
if let Some(file_name) = path.file_name() {
|
|
if create_parent && tokio::fs::metadata(parent).await.is_err() {
|
|
return Ok(create_canonical_folder(parent).await?.join(file_name));
|
|
} else {
|
|
return Ok(tokio::fs::canonicalize(parent)
|
|
.await
|
|
.with_context(|| parent.display().to_string())?
|
|
.join(file_name));
|
|
}
|
|
}
|
|
}
|
|
tokio::fs::canonicalize(&path)
|
|
.await
|
|
.with_context(|| path.display().to_string())
|
|
}
|
|
|
|
#[pin_project::pin_project(PinnedDrop)]
|
|
pub struct NonDetachingJoinHandle<T>(#[pin] JoinHandle<T>);
|
|
impl<T> NonDetachingJoinHandle<T> {
|
|
pub async fn wait_for_abort(self) -> Result<T, JoinError> {
|
|
self.abort();
|
|
self.await
|
|
}
|
|
}
|
|
impl<T> From<JoinHandle<T>> for NonDetachingJoinHandle<T> {
|
|
fn from(t: JoinHandle<T>) -> Self {
|
|
NonDetachingJoinHandle(t)
|
|
}
|
|
}
|
|
|
|
impl<T> Deref for NonDetachingJoinHandle<T> {
|
|
type Target = JoinHandle<T>;
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
impl<T> DerefMut for NonDetachingJoinHandle<T> {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.0
|
|
}
|
|
}
|
|
#[pin_project::pinned_drop]
|
|
impl<T> PinnedDrop for NonDetachingJoinHandle<T> {
|
|
fn drop(self: std::pin::Pin<&mut Self>) {
|
|
let this = self.project();
|
|
this.0.into_ref().get_ref().abort()
|
|
}
|
|
}
|
|
impl<T> Future for NonDetachingJoinHandle<T> {
|
|
type Output = Result<T, JoinError>;
|
|
fn poll(
|
|
self: std::pin::Pin<&mut Self>,
|
|
cx: &mut std::task::Context<'_>,
|
|
) -> std::task::Poll<Self::Output> {
|
|
let this = self.project();
|
|
this.0.poll(cx)
|
|
}
|
|
}
|
|
|
|
pub struct AtomicFile {
|
|
tmp_path: PathBuf,
|
|
path: PathBuf,
|
|
file: Option<File>,
|
|
}
|
|
impl AtomicFile {
|
|
pub async fn new(
|
|
path: impl AsRef<Path> + Send + Sync,
|
|
tmp_path: Option<impl AsRef<Path> + Send + Sync>,
|
|
) -> Result<Self, Error> {
|
|
let path = canonicalize(&path, true).await?;
|
|
let tmp_path = if let Some(tmp_path) = tmp_path {
|
|
canonicalize(&tmp_path, true).await?
|
|
} else {
|
|
to_tmp_path(&path)?
|
|
};
|
|
let file = File::create(&tmp_path)
|
|
.await
|
|
.with_context(|| tmp_path.display().to_string())?;
|
|
Ok(Self {
|
|
tmp_path,
|
|
path,
|
|
file: Some(file),
|
|
})
|
|
}
|
|
|
|
pub async fn rollback(mut self) -> Result<(), Error> {
|
|
drop(self.file.take());
|
|
tokio::fs::remove_file(&self.tmp_path)
|
|
.await
|
|
.with_context(|| format!("rm {}", self.tmp_path.display()))?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn save(mut self) -> Result<(), Error> {
|
|
use tokio::io::AsyncWriteExt;
|
|
if let Some(file) = self.file.as_mut() {
|
|
file.flush().await?;
|
|
file.shutdown().await?;
|
|
file.sync_all().await?;
|
|
}
|
|
drop(self.file.take());
|
|
tokio::fs::rename(&self.tmp_path, &self.path)
|
|
.await
|
|
.with_context(|| {
|
|
format!("mv {} -> {}", self.tmp_path.display(), self.path.display())
|
|
})?;
|
|
Ok(())
|
|
}
|
|
}
|
|
impl std::ops::Deref for AtomicFile {
|
|
type Target = File;
|
|
fn deref(&self) -> &Self::Target {
|
|
self.file.as_ref().unwrap()
|
|
}
|
|
}
|
|
impl std::ops::DerefMut for AtomicFile {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
self.file.as_mut().unwrap()
|
|
}
|
|
}
|
|
impl Drop for AtomicFile {
|
|
fn drop(&mut self) {
|
|
if let Some(file) = self.file.take() {
|
|
drop(file);
|
|
let path = std::mem::take(&mut self.tmp_path);
|
|
tokio::spawn(async move { tokio::fs::remove_file(path).await.log_err() });
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct TimedResource<T: 'static + Send> {
|
|
handle: NonDetachingJoinHandle<Option<T>>,
|
|
ready: oneshot::Sender<()>,
|
|
}
|
|
impl<T: 'static + Send> TimedResource<T> {
|
|
pub fn new(resource: T, timer: Duration) -> Self {
|
|
let (send, recv) = oneshot::channel();
|
|
let handle = tokio::spawn(async move {
|
|
tokio::select! {
|
|
_ = tokio::time::sleep(timer) => {
|
|
drop(resource);
|
|
None
|
|
},
|
|
_ = recv => Some(resource),
|
|
}
|
|
});
|
|
Self {
|
|
handle: handle.into(),
|
|
ready: send,
|
|
}
|
|
}
|
|
|
|
pub fn new_with_destructor<
|
|
Fn: FnOnce(T) -> Fut + Send + 'static,
|
|
Fut: Future<Output = ()> + Send,
|
|
>(
|
|
resource: T,
|
|
timer: Duration,
|
|
destructor: Fn,
|
|
) -> Self {
|
|
let (send, recv) = oneshot::channel();
|
|
let handle = tokio::spawn(async move {
|
|
tokio::select! {
|
|
_ = tokio::time::sleep(timer) => {
|
|
destructor(resource).await;
|
|
None
|
|
},
|
|
_ = recv => Some(resource),
|
|
}
|
|
});
|
|
Self {
|
|
handle: handle.into(),
|
|
ready: send,
|
|
}
|
|
}
|
|
|
|
pub async fn get(self) -> Option<T> {
|
|
let _ = self.ready.send(());
|
|
self.handle.await.unwrap()
|
|
}
|
|
|
|
pub fn is_timed_out(&self) -> bool {
|
|
self.ready.is_closed()
|
|
}
|
|
}
|
|
|
|
pub async fn spawn_local<
|
|
T: 'static + Send,
|
|
F: FnOnce() -> Fut + Send + 'static,
|
|
Fut: Future<Output = T> + 'static,
|
|
>(
|
|
fut: F,
|
|
) -> NonDetachingJoinHandle<T> {
|
|
let (send, recv) = tokio::sync::oneshot::channel();
|
|
std::thread::spawn(move || {
|
|
tokio::runtime::Builder::new_current_thread()
|
|
.enable_all()
|
|
.build()
|
|
.unwrap()
|
|
.block_on(async move {
|
|
let set = LocalSet::new();
|
|
send.send(set.spawn_local(fut()).into())
|
|
.unwrap_or_else(|_| unreachable!());
|
|
set.await
|
|
})
|
|
});
|
|
recv.await.unwrap()
|
|
}
|