Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/ad_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tokio = ["dep:tokio", "ninep/tokio"]
[dependencies]
ad_event = { version = "0.4", path = "../ad_event" }
ninep = { version = "0.6", path = "../ninep", default-features = false }
tokio = { version = "1", features = ["macros", "net", "io-util", "rt", "sync"], optional = true }
tokio = { version = "1", features = ["macros", "net", "io-util", "rt", "sync", "time"], optional = true }

[dev-dependencies]
ad-editor = { path = "../../" }
Expand Down
4 changes: 4 additions & 0 deletions crates/ad_client/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{
os::unix::net::UnixStream,
path::Path,
str::FromStr,
thread::sleep,
time::Duration,
};

mod event;
Expand Down Expand Up @@ -221,6 +223,8 @@ impl Client {
}

fn _id_for_path(&mut self, path: &str) -> Result<usize> {
sleep(Duration::from_millis(5));

for BufferMeta { id, filename } in self.open_buffers()?.into_iter() {
if filename.ends_with(path) {
return Ok(id);
Expand Down
5 changes: 3 additions & 2 deletions crates/ad_client/src/tokio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! An asynchronous client implementation.
use crate::{BufferMeta, LogEvent, MiniBufferSelection, SessionMeta, parse_bufid};
use ninep::tokio::client::{Error, ReadLineStream, Result, UnixClient};
use std::{env, io, path::Path, str::FromStr};
use tokio::net::UnixStream;
use std::{env, io, path::Path, str::FromStr, time::Duration};
use tokio::{net::UnixStream, time::sleep};

mod event;

Expand Down Expand Up @@ -232,6 +232,7 @@ impl Client {
}

async fn _id_for_path(&mut self, path: &str) -> Result<usize> {
sleep(Duration::from_millis(5)).await;
for BufferMeta { id, filename } in self.open_buffers().await?.into_iter() {
if filename.ends_with(path) {
return Ok(id);
Expand Down
9 changes: 6 additions & 3 deletions crates/ninep/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ include = [
]

[features]
default = ["tokio"]
default = ["tokio", "util_fs"]
tokio = ["dep:tokio"]
util_fs = ["dep:uzers"]

[dependencies]
bitflags = "2"
tokio = { version = "1", features = ["macros", "net", "io-util", "rt", "sync"], optional = true }
jiff = "0.2"
simple_coro = "0.1.5"
jiff = "0.2.24"
tokio = { version = "1", features = ["macros", "net", "io-util", "rt", "sync"], optional = true }
uzers = { version = "0.12", optional = true }

[dev-dependencies]
assert_fs = "1.1"
lexopt = "0.3.2"
simple_test_case = "1"
tokio = { version = "1", features = ["macros", "net", "io-util", "rt", "sync", "time", "rt-multi-thread", "net", "io-util"] }
2 changes: 1 addition & 1 deletion crates/ninep/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Args {
};

let (socket_path, path) = match path.split_once('/') {
Some((ns, path)) => (ns, path),
Some((socket_path, path)) => (socket_path, path),
None => (path.as_str(), "/"),
};

Expand Down
29 changes: 29 additions & 0 deletions crates/ninep/examples/local_proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use ninep::{
sync::server::Server,
util::{hook::HookFs, local_proxy::LocalProxyFs},
};
use std::env::{args, current_dir};

fn main() {
let path = args().nth(1).expect("must provide a path to bind to");
let chatty = args().nth(2).as_deref() == Some("--chatty");

let fs = HookFs::new(LocalProxyFs::new(path).unwrap(), move |op| {
if chatty {
println!("{op:?}");
}

Ok(())
});

let s = Server::new(fs);
let socket_path = current_dir().unwrap().join("proxy-fs");

println!(
"starting local-proxy-fs file server at {}",
socket_path.display()
);
if s.serve_socket_with_custom_path(socket_path).join().is_err() {
eprintln!("server thread died");
}
}
25 changes: 25 additions & 0 deletions crates/ninep/examples/ramfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! A simple in-memory filesystem that allows clients to read and write arbitrary file paths.
use ninep::{
sync::server::Server,
util::{hook::HookFs, ram::RamFs},
};
use std::env::{args, current_dir};

fn main() {
let chatty = args().nth(1).as_deref() == Some("--chatty");
let fs = HookFs::new(RamFs::new(env!("USER"), "group"), move |op| {
if chatty {
println!("{op:?}");
}

Ok(())
});

let s = Server::new(fs);
let socket_path = current_dir().unwrap().join("ramfs");

println!("starting ram-fs file server at {}", socket_path.display());
if s.serve_socket_with_custom_path(socket_path).join().is_err() {
eprintln!("server thread died");
}
}
11 changes: 8 additions & 3 deletions crates/ninep/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl Mode {
}

fn is_allowed(&self, read: bool, write: bool, exec: bool) -> bool {
let m = Mode::new(self.bits() & 0x03); // Mask off the additional truncate and remove bits
let m = self.base(); // Mask off the additional truncate and remove bits

(m == Mode::READ && read)
|| (m == Mode::WRITE && write)
Expand All @@ -213,12 +213,17 @@ impl Mode {
|| false
}

/// The base [Mode] without [Mode::TRUNCATE] and [Mode::REMOVE_ON_CLOSE] bits.
pub fn base(&self) -> Mode {
Mode::new(self.bits() & 0x03)
}

pub(crate) fn allows_read(&self) -> bool {
*self == Mode::READ || *self == Mode::READ_WRITE
self.base() == Mode::READ || self.base() == Mode::READ_WRITE
}

pub(crate) fn allows_write(&self) -> bool {
*self == Mode::WRITE || *self == Mode::READ_WRITE
self.base() == Mode::WRITE || self.base() == Mode::READ_WRITE
}
}

Expand Down
58 changes: 51 additions & 7 deletions crates/ninep/src/fs/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ where
self.with_nodes_mut(|nodes| nodes.remove(qid))
}

/// Attempt to map a path within this file tree to a qid.
///
/// Returns `Some(qid)` for a known path, otherwise `None`.
pub fn qid_for_path(&self, path: &str) -> Option<u64> {
if !path.starts_with('/') {
return None;
}

let mut qid = 0;

// Need to skip the empty string from the leading slash
for elem in path.split('/').skip(1) {
qid = self.walk_one(qid, elem).ok()?.path;
}

Some(qid)
}

/// Whether or not this file tree contains the given qid
pub fn contains_qid(&self, qid: u64) -> bool {
self.with_nodes(|nodes| nodes.entries.contains_key(&qid))
}

/// Run a closure with access to the [File] associated with the given `qid`.
pub fn with_file<F, U>(&self, qid: u64, f: F) -> Result<U>
where
Expand Down Expand Up @@ -123,13 +146,23 @@ where
/// A simple file implementation for use in a [FileTree].
#[derive(Debug, Clone)]
pub struct File<T> {
stat: Stat,
parent: Option<u64>,
/// The stat associated with this [File] node.
pub stat: Stat,
/// User defined additional data per [File] node.
pub aux: T,
}

impl<T> File<T> {
fn new(qid: Qid, name: &str, owner: &str, group: &str, perms: Perm, aux: T) -> Self {
fn new(
qid: Qid,
name: &str,
owner: &str,
group: &str,
perms: Perm,
aux: T,
parent: Option<u64>,
) -> Self {
File {
stat: Stat {
qid,
Expand All @@ -143,12 +176,15 @@ impl<T> File<T> {
last_modified_by: owner.into(),
},
aux,
parent,
}
}

/// The [Stat] for this file.
pub fn stat(&self) -> &Stat {
&self.stat
/// The `qid` of the parent node for this file.
///
/// Returns [None] for the root node.
pub fn parent(&self) -> Option<u64> {
self.parent
}

/// Attempt to apply a [WStat] to the [Stat] of this file.
Expand Down Expand Up @@ -179,7 +215,7 @@ where
T: Send + Sync + 'static,
{
fn new(owner: &str, group: &str, perms: Perm, aux: T) -> Self {
let root = File::new(Qid::dir(0), "/", owner, group, perms, aux);
let root = File::new(Qid::dir(0), "/", owner, group, perms, aux, None);

Self {
entries: BTreeMap::from_iter([(0, root)]),
Expand Down Expand Up @@ -231,7 +267,15 @@ where

self.entries.insert(
qid_path,
File::new(qid, name, &pstat.owner, &pstat.group, perms, aux),
File::new(
qid,
name,
&pstat.owner,
&pstat.group,
perms,
aux,
Some(parent),
),
);
self.children
.entry(pstat.qid.path)
Expand Down
2 changes: 2 additions & 0 deletions crates/ninep/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
pub mod fs;
pub mod sansio;
pub mod sync;
#[cfg(feature = "util_fs")]
pub mod util;

#[cfg(feature = "tokio")]
pub mod tokio;
Expand Down
Loading
Loading