Skip to content

Commit

Permalink
Preparing for new events command.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Nov 30, 2023
1 parent 248f888 commit 6c93d05
Show file tree
Hide file tree
Showing 27 changed files with 355 additions and 138 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ test-utils = ["sos-net/test-utils"]
[workspace.dependencies]
csv-async = { version = "1", features = ["tokio", "with_serde"] }

[workspace.dependencies.binary-stream]
version = "8.0.0"
features = ["async"]
#path = "../../../../binary-stream"

[dependencies]
csv-async.workspace = true
binary-stream.workspace = true
thiserror = "1"
tracing = "0.1"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
Expand Down
10 changes: 8 additions & 2 deletions src/cli/sos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use std::path::PathBuf;

use crate::{
commands::{
account, audit, changes, check, device, folder, secret,
account, audit, changes, check, device, events, folder, secret,
security_report::{self, SecurityReportFormat},
shell, AccountCommand, AuditCommand, CheckCommand, DeviceCommand,
FolderCommand, SecretCommand,
EventsCommand, FolderCommand, SecretCommand,
},
Result,
};
Expand Down Expand Up @@ -124,6 +124,11 @@ pub enum Command {
#[clap(subcommand)]
cmd: CheckCommand,
},
/// Inspect event records.
Events {
#[clap(subcommand)]
cmd: EventsCommand,
},
/// Interactive login shell.
Shell {
/// Folder name or identifier.
Expand Down Expand Up @@ -179,6 +184,7 @@ pub async fn run() -> Result<()> {
changes::run(server, server_public_key, account).await?
}
Command::Check { cmd } => check::run(cmd).await?,
Command::Events { cmd } => events::run(cmd).await?,
Command::Shell { account, folder } => {
shell::run(account, folder).await?
}
Expand Down
46 changes: 1 addition & 45 deletions src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use clap::Subcommand;
use std::path::PathBuf;

use sos_net::sdk::{
commit::{
event_log_commit_tree_file, vault_commit_tree_file, CommitHash,
},
events::{EventRecord, FolderEventLog, WriteEvent},
commit::{event_log_commit_tree_file, vault_commit_tree_file},
formats::vault_stream,
hex,
uuid::Uuid,
Expand Down Expand Up @@ -42,15 +39,6 @@ pub enum Command {
#[clap(short, long)]
verbose: bool,

/// Log file path.
file: PathBuf,
},
/// Print event log records.
Events {
/// Reverse the iteration direction.
#[clap(short, long)]
reverse: bool,

/// Log file path.
file: PathBuf,
},
Expand All @@ -66,9 +54,6 @@ pub async fn run(cmd: Command) -> Result<()> {
Command::Log { verbose, file } => {
verify_log(file, verbose).await?;
}
Command::Events { file, reverse } => {
print_events(file, reverse).await?;
}
}

Ok(())
Expand Down Expand Up @@ -109,35 +94,6 @@ async fn verify_log(file: PathBuf, verbose: bool) -> Result<()> {
Ok(())
}

/// Print the events of a log file.
async fn print_events(file: PathBuf, reverse: bool) -> Result<()> {
if !vfs::metadata(&file).await?.is_file() {
return Err(Error::NotFile(file));
}

let event_log = FolderEventLog::new(&file).await?;
let mut it = if reverse {
event_log.iter().await?.rev()
} else {
event_log.iter().await?
};
let divider = "-".repeat(80);

while let Some(record) = it.next_entry().await? {
println!("{}", divider);
println!(" time: {}", record.time());
println!("before: {}", CommitHash(record.last_commit()));
println!("commit: {}", CommitHash(record.commit()));
let event_buffer = event_log.read_event_buffer(&record).await?;
let event_record: EventRecord = (record, event_buffer).into();
let event = event_record.decode_event::<WriteEvent>().await?;
println!(" event: {}", event.event_kind());
}
println!("{}", divider);

Ok(())
}

/// Print a vault header.
pub async fn header(vault: PathBuf) -> Result<()> {
if !vfs::metadata(&vault).await?.is_file() {
Expand Down
100 changes: 100 additions & 0 deletions src/commands/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use clap::Subcommand;
use std::path::PathBuf;

use binary_stream::futures::{Decodable, Encodable};
use sos_net::sdk::{
commit::CommitHash,
events::{
AccountEvent, AccountEventLog, EventLogFile, EventRecord, FileEvent,
FileEventLog, FolderEventLog, LogEvent, WriteEvent,
},
vfs,
};

use crate::{Error, Result};

#[derive(Subcommand, Debug)]
pub enum Command {
/// Print account event log records.
Account {
/// Reverse the iteration direction.
#[clap(short, long)]
reverse: bool,

/// Log file path.
file: PathBuf,
},
/// Print folder event log records.
Folder {
/// Reverse the iteration direction.
#[clap(short, long)]
reverse: bool,

/// Log file path.
file: PathBuf,
},
/// Print file event log records.
File {
/// Reverse the iteration direction.
#[clap(short, long)]
reverse: bool,

/// Log file path.
file: PathBuf,
},
}

pub async fn run(cmd: Command) -> Result<()> {
match cmd {
Command::Account { file, reverse } => {
if !vfs::metadata(&file).await?.is_file() {
return Err(Error::NotFile(file));
}
let event_log = AccountEventLog::new_account(&file).await?;
print_events::<AccountEvent>(event_log, reverse).await?;
}
Command::Folder { file, reverse } => {
if !vfs::metadata(&file).await?.is_file() {
return Err(Error::NotFile(file));
}
let event_log = FolderEventLog::new_folder(&file).await?;
print_events::<WriteEvent>(event_log, reverse).await?;
}
Command::File { file, reverse } => {
if !vfs::metadata(&file).await?.is_file() {
return Err(Error::NotFile(file));
}
let event_log = FileEventLog::new_file(&file).await?;
print_events::<FileEvent>(event_log, reverse).await?;
}
}

Ok(())
}

/// Print the events of a log file.
async fn print_events<T: Default + Encodable + Decodable + LogEvent>(
event_log: EventLogFile<T>,
reverse: bool,
) -> Result<()> {
let mut it = if reverse {
event_log.iter().await?.rev()
} else {
event_log.iter().await?
};
let divider = "-".repeat(80);

while let Some(record) = it.next_entry().await? {
println!("{}", divider);
println!(" time: {}", record.time());
println!("before: {}", CommitHash(record.last_commit()));
println!("commit: {}", CommitHash(record.commit()));
let event_buffer = event_log.read_event_buffer(&record).await?;
let event_record: EventRecord = (record, event_buffer).into();
let event = event_record.decode_event::<T>().await?;
println!(" event: {}", event.event_kind());
}
println!("{}", divider);

Ok(())
}
4 changes: 3 additions & 1 deletion src/commands/folder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use clap::Subcommand;

use human_bytes::human_bytes;
use sos_net::sdk::{account::AccountRef, hex, vault::FolderRef};
use sos_net::sdk::{
account::AccountRef, events::LogEvent, hex, vault::FolderRef,
};

use crate::{
helpers::{
Expand Down
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod audit;
pub mod changes;
pub mod check;
pub mod device;
pub mod events;
pub mod folder;
pub mod generate_keypair;
pub mod secret;
Expand All @@ -14,5 +15,6 @@ pub use account::Command as AccountCommand;
pub use audit::Command as AuditCommand;
pub use check::Command as CheckCommand;
pub use device::Command as DeviceCommand;
pub use events::Command as EventsCommand;
pub use folder::Command as FolderCommand;
pub use secret::Command as SecretCommand;
5 changes: 4 additions & 1 deletion src/helpers/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,16 @@ pub async fn verify(user: Owner) -> Result<bool> {
/// List local accounts.
pub async fn list_accounts(verbose: bool) -> Result<()> {
let accounts = AccountsList::list_accounts(None).await?;
for account in accounts {
for account in &accounts {
if verbose {
println!("{} {}", account.address(), account.label());
} else {
println!("{}", account.label());
}
}
if accounts.is_empty() {
println!("no accounts yet");
}
Ok(())
}

Expand Down
5 changes: 4 additions & 1 deletion workspace/net/src/client/account/network_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,9 @@ impl NetworkAccount {
label: &str,
id: Option<&SecretId>,
) -> Result<bool> {
Ok(self.account.document_exists_in_folder(vault_id, label, id).await?)
Ok(self
.account
.document_exists_in_folder(vault_id, label, id)
.await?)
}
}
15 changes: 10 additions & 5 deletions workspace/net/src/server/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ impl FileSystemBackend {
let id = *summary.id();

let mut event_log_file =
FolderEventLog::new(&event_log_path)
.await?;
FolderEventLog::new_folder(
&event_log_path,
)
.await?;
event_log_file.load_tree().await?;

// Store these file paths so locks
Expand Down Expand Up @@ -265,7 +267,8 @@ impl FileSystemBackend {
vfs::write(&vault_path, vault).await?;

// Create the event log file
let mut event_log = FolderEventLog::new(&event_log_path).await?;
let mut event_log =
FolderEventLog::new_folder(&event_log_path).await?;
let event = WriteEvent::CreateVault(vault.to_vec());
event_log.append_event(&event).await?;

Expand Down Expand Up @@ -477,7 +480,8 @@ impl BackendHandler for FileSystemBackend {

// Prepare a temp file with the new event log records
let temp = NamedTempFile::new()?;
let mut temp_event_log = FolderEventLog::new(temp.path()).await?;
let mut temp_event_log =
FolderEventLog::new_folder(temp.path()).await?;
temp_event_log.apply(events.iter().collect()).await?;

let expected_root = temp_event_log
Expand Down Expand Up @@ -624,7 +628,8 @@ impl BackendHandler for FileSystemBackend {
.get_mut(vault_id)
.ok_or_else(|| Error::VaultNotExist(*vault_id))?;

*event_log = FolderEventLog::new(&original_event_log).await?;
*event_log =
FolderEventLog::new_folder(&original_event_log).await?;
event_log.load_tree().await?;
let root = event_log
.tree()
Expand Down
6 changes: 1 addition & 5 deletions workspace/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ device = []
security-report = []

[dependencies]
binary-stream.workspace = true
sos-vfs = "0.2"
async-trait = "0.1"
async-recursion = "1"
Expand Down Expand Up @@ -79,11 +80,6 @@ mpc-protocol = "0.4.1"

vsss-rs = {version = "3", optional = true }

[dependencies.binary-stream]
version = "8.0.0"
features = ["async"]
#path = "../../../../binary-stream"

[dependencies.probly-search]
version = "2.0.0-alpha-2"
#path = "../../../../forks/probly-search"
Expand Down
4 changes: 2 additions & 2 deletions workspace/sdk/src/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ impl<D> Account<D> {
user,
storage: Arc::new(RwLock::new(storage)),
account_log: Arc::new(RwLock::new(
AccountEventLog::new(account_events).await?,
AccountEventLog::new_account(account_events).await?,
)),
file_log: Arc::new(RwLock::new(
FileEventLog::new(file_events).await?,
FileEventLog::new_file(file_events).await?,
)),
});

Expand Down
2 changes: 1 addition & 1 deletion workspace/sdk/src/account/archive/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ impl AccountBackup {
let create_vault = WriteEvent::CreateVault(buffer.clone());
event_log_events.push(create_vault);
let mut event_log =
FolderEventLog::new(event_log_path).await?;
FolderEventLog::new_folder(event_log_path).await?;
event_log.apply(event_log_events.iter().collect()).await?;
}

Expand Down
3 changes: 2 additions & 1 deletion workspace/sdk/src/account/local_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ impl FolderStorage {
vault: Option<Vault>,
) -> Result<()> {
let event_log_path = self.event_log_path(summary);
let mut event_log = FolderEventLog::new(&event_log_path).await?;
let mut event_log =
FolderEventLog::new_folder(&event_log_path).await?;

if let Some(vault) = &vault {
// Must truncate the event log so that importing vaults
Expand Down
3 changes: 2 additions & 1 deletion workspace/sdk/src/commit/integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ where
let mut file = vfs::File::open(event_log_file.as_ref()).await?.compat();
let mut reader = BinaryReader::new(&mut file, encoding_options());

let event_log = FolderEventLog::new(event_log_file.as_ref()).await?;
let event_log =
FolderEventLog::new_folder(event_log_file.as_ref()).await?;
let mut it = event_log.iter().await?;
let mut last_checksum: Option<[u8; 32]> = None;

Expand Down
Loading

0 comments on commit 6c93d05

Please sign in to comment.