cargo check

This commit is contained in:
Sebastian Moser
2024-08-11 23:48:46 +02:00
parent f24c46338e
commit eb1b94a650
10 changed files with 180 additions and 13 deletions

View File

@@ -45,6 +45,10 @@ impl FileStore {
Ok(FileStore { path: Path::new(&path).to_owned() })
}
pub fn store_is_opened(store_path: String) -> MizeResult<bool> {
Ok(valid_pid_file(Path::new(&store_path))?.is_some())
}
}
impl Store for FileStore {

View File

@@ -2,7 +2,7 @@ use ciborium::Value as CborValue;
use serde::Deserialize;
use std::env::VarError;
use std::fs;
use tracing::{debug, error, warn, trace};
use tracing::{debug, error, info, trace, warn};
use clap::ArgMatches;
use std::path::Path;
@@ -12,6 +12,7 @@ use crate::instance::Instance;
use crate::error::{IntoMizeResult, MizeError, MizeResult};
use crate::item::{ItemData, IntoItemData};
use crate::memstore::MemStore;
use crate::mize_err;
use crate::platform::os::unix_socket::UnixListener;
use self::fsstore::FileStore;
@@ -60,10 +61,27 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> {
}
};
////// if a config.store is set, upgrade to the filestore there
let store_path = instance.get("0/config/store_path")?.value_string()?;
////// if a config.store_path is set, upgrade to the filestore there
let mut store_path = instance.get("0/config/store_path")?.value_string()?;
if store_path != "" {
if store_path == "" {
let home_dir = env!("HOME");
if home_dir == "" {
return Err(mize_err!("env var $HOME empty"));
}
// the default store_path: $HOME/.mize
store_path = home_dir.to_owned() + "/.mize";
}
if FileStore::store_is_opened(store_path.to_owned())? {
// if the store is already opened, connect to the instance, that opened it and join
// it's namespace
info!("CONNECTING");
return Ok(());
} else {
// else open it ourselves
let file_store = FileStore::new(store_path.as_str())?;
instance.migrate_to_store(Box::new(file_store))?;

View File

@@ -1,11 +1,11 @@
use std::fs;
use std::path::{Path, PathBuf};
use tokio::net::UnixListener as TokioUnixListener;
use tokio::net::{UnixListener as TokioUnixListener, UnixStream};
use tokio::net::unix::{OwnedReadHalf, OwnedWriteHalf};
use tokio::io::{Interest, AsyncReadExt, AsyncWriteExt};
use crossbeam::channel::{Receiver, Sender, unbounded};
use ciborium::Value as CborValue;
use tracing::warn;
use tracing::{info, warn};
use crate::instance::connection::{ConnListener, Connection};
use crate::instance::{self, Instance};
@@ -20,8 +20,35 @@ impl UnixListener {
pub fn new(store_path: PathBuf) -> MizeResult<UnixListener> {
Ok(UnixListener { sock_path: store_path.join("sock").to_owned() })
}
}
pub fn connect(instance: &mut Instance, store_path: PathBuf) -> MizeResult<()> {
instance.spawn_async("unix_connection", connect_async(instance.clone(), store_path) );
Ok(())
}
async fn connect_async(mut instance: Instance, store_path: PathBuf) -> MizeResult<()> {
let mut stream = UnixStream::connect(store_path).await?;
let (read_half, write_half) = stream.split();
let (recieve_tx, recieve_rx) = unbounded::<MizeMessage>();
let (send_tx, send_rx) = unbounded::<MizeMessage>();
let conn_id = instance.new_connection(recieve_rx, send_tx)?;
let ns_of_peer = instance.get(format!("inst/con_by_id/{}/peer/0/config/namespace", conn_id))?.value_string()?;
info!("connect_async ... ns_of_peer: {}", ns_of_peer);
//instance.connection_set_namespace(conn_id, namespace)
Ok(())
}
impl ConnListener for UnixListener {
fn listen(self, mut instance: Instance) -> MizeResult<()> {
instance.spawn_async("unix listen async", unix_listen(self, instance.clone()));