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
11 changes: 9 additions & 2 deletions examples/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#![deny(clippy::unimplemented)]
#![forbid(unsafe_code)]

use libc::{SIGHUP, SIGINT, SIGTERM};
use polyfuse::{
mount::MountOptions,
notify::Notifier as _,
Expand All @@ -22,6 +23,7 @@ use anyhow::{anyhow, ensure, Context as _, Result};
use chrono::Local;
use dashmap::DashMap;
use rustix::{io::Errno, param::page_size};
use signal_hook::iterator::Signals;
use std::{
io::{self, prelude::*},
path::PathBuf,
Expand Down Expand Up @@ -51,6 +53,13 @@ fn main() -> Result<()> {
let conn = &conn;
let session = &session;
thread::scope(|scope| -> Result<()> {
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
scope.spawn(move || {
if let Some(_sig) = signals.forever().next() {
let _ = mount.unmount();
}
});

// Spawn a task that beats the heart.
scope.spawn({
let fs = fs.clone();
Expand Down Expand Up @@ -127,8 +136,6 @@ fn main() -> Result<()> {
Ok(())
})?;

mount.unmount()?;

Ok(())
}

Expand Down
11 changes: 9 additions & 2 deletions examples/heartbeat_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#![deny(clippy::unimplemented, clippy::todo)]
#![forbid(unsafe_code)]

use libc::{SIGHUP, SIGINT, SIGTERM};
use polyfuse::{
mount::MountOptions,
notify::Notifier as _,
Expand All @@ -21,6 +22,7 @@ use polyfuse::{
use anyhow::{ensure, Context as _, Result};
use chrono::Local;
use rustix::io::Errno;
use signal_hook::iterator::Signals;
use std::{
io, mem,
os::unix::prelude::*,
Expand Down Expand Up @@ -66,6 +68,13 @@ fn main() -> Result<()> {
let session = &session;
let conn = &conn;
thread::scope(|scope| -> Result<()> {
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
scope.spawn(move || {
if let Some(_sig) = signals.forever().next() {
let _ = mount.unmount();
}
});

// spawn heartbeat thread.
scope.spawn({
let fs = fs.clone();
Expand Down Expand Up @@ -152,8 +161,6 @@ fn main() -> Result<()> {
Ok(())
})?;

mount.unmount()?;

Ok(())
}

Expand Down
127 changes: 69 additions & 58 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![deny(clippy::unimplemented)]
#![forbid(unsafe_code)]

use libc::{SIGHUP, SIGINT, SIGTERM};
use polyfuse::{
mount::MountOptions,
op::Operation,
Expand All @@ -16,7 +17,8 @@ use rustix::{
io::Errno,
process::{getgid, getuid},
};
use std::{os::unix::prelude::*, path::PathBuf, time::Duration};
use signal_hook::iterator::Signals;
use std::{os::unix::prelude::*, path::PathBuf, thread, time::Duration};

const TTL: Duration = Duration::from_secs(60 * 60 * 24 * 365);
const HELLO_INO: NodeID = match NodeID::from_raw(2) {
Expand All @@ -39,78 +41,87 @@ fn main() -> Result<()> {

let fs = Hello::new();

let mut buf = session.new_splice_buffer()?;
while session.recv_request(&conn, &mut buf)? {
let (req, op, _remains) = session.decode(&conn, &mut buf)?;
match op {
Operation::Lookup(op) => match op.parent {
NodeID::ROOT if op.name.as_bytes() == HELLO_FILENAME.as_bytes() => {
req.reply_entry(Some(HELLO_INO), &fs.hello_attr(), 0, Some(TTL), Some(TTL))?
}
_ => req.reply_error(Errno::NOENT)?,
},

Operation::Getattr(op) => {
let attr = match op.ino {
NodeID::ROOT => fs.root_attr(),
HELLO_INO => fs.hello_attr(),
_ => Err(Errno::NOENT)?,
};
req.reply_attr(&attr, Some(TTL))?;
thread::scope(|scope| -> Result<()> {
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
scope.spawn(move || {
if let Some(_sig) = signals.forever().next() {
let _ = mount.unmount();
}

Operation::Read(op) => {
match op.ino {
HELLO_INO => (),
NodeID::ROOT => {
req.reply_error(Errno::ISDIR)?;
continue;
}
_ => {
req.reply_error(Errno::NOENT)?;
continue;
});

let mut buf = session.new_splice_buffer()?;
while session.recv_request(&conn, &mut buf)? {
let (req, op, _remains) = session.decode(&conn, &mut buf)?;
match op {
Operation::Lookup(op) => match op.parent {
NodeID::ROOT if op.name.as_bytes() == HELLO_FILENAME.as_bytes() => {
req.reply_entry(Some(HELLO_INO), &fs.hello_attr(), 0, Some(TTL), Some(TTL))?
}
_ => req.reply_error(Errno::NOENT)?,
},

Operation::Getattr(op) => {
let attr = match op.ino {
NodeID::ROOT => fs.root_attr(),
HELLO_INO => fs.hello_attr(),
_ => Err(Errno::NOENT)?,
};
req.reply_attr(&attr, Some(TTL))?;
}

let mut data: &[u8] = &[];
Operation::Read(op) => {
match op.ino {
HELLO_INO => (),
NodeID::ROOT => {
req.reply_error(Errno::ISDIR)?;
continue;
}
_ => {
req.reply_error(Errno::NOENT)?;
continue;
}
}

let offset = op.offset as usize;
if offset < HELLO_CONTENT.len() {
let size = op.size as usize;
data = &HELLO_CONTENT[offset..];
data = &data[..std::cmp::min(data.len(), size)];
}
let mut data: &[u8] = &[];

req.reply_bytes(data)?;
}
let offset = op.offset as usize;
if offset < HELLO_CONTENT.len() {
let size = op.size as usize;
data = &HELLO_CONTENT[offset..];
data = &data[..std::cmp::min(data.len(), size)];
}

Operation::Readdir(op) => {
if op.ino != NodeID::ROOT {
req.reply_error(Errno::NOTDIR)?;
continue;
req.reply_bytes(data)?;
}

let mut buf = DirEntryBuf::new(op.size as usize);
for (i, entry) in fs.dir_entries().skip(op.offset as usize) {
let full = buf.push_entry(
entry.name.as_ref(), //
entry.ino,
entry.typ,
i + 1,
);
if full {
break;
Operation::Readdir(op) => {
if op.ino != NodeID::ROOT {
req.reply_error(Errno::NOTDIR)?;
continue;
}

let mut buf = DirEntryBuf::new(op.size as usize);
for (i, entry) in fs.dir_entries().skip(op.offset as usize) {
let full = buf.push_entry(
entry.name.as_ref(), //
entry.ino,
entry.typ,
i + 1,
);
if full {
break;
}
}

req.reply_dir(&buf)?;
}

req.reply_dir(&buf)?;
_ => req.reply_error(Errno::NOSYS)?,
}

_ => req.reply_error(Errno::NOSYS)?,
}
}

mount.unmount()?;
Ok(())
})?;

Ok(())
}
Expand Down
11 changes: 9 additions & 2 deletions examples/poll.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use libc::{SIGHUP, SIGINT, SIGTERM};
use polyfuse::{
mount::MountOptions,
notify::Notifier as _,
Expand All @@ -14,6 +15,7 @@ use rustix::{
io::Errno,
process::{getgid, getuid},
};
use signal_hook::iterator::Signals;
use std::{
collections::HashMap,
path::PathBuf,
Expand Down Expand Up @@ -49,6 +51,13 @@ fn main() -> Result<()> {
let conn = &conn;
let session = &session;
thread::scope(|scope| -> Result<()> {
let mut signals = Signals::new([SIGTERM, SIGHUP, SIGINT])?;
scope.spawn(move || {
if let Some(_sig) = signals.forever().next() {
let _ = mount.unmount();
}
});

let mut buf = session.new_splice_buffer()?;
while session.recv_request(conn, &mut buf)? {
let (req, op, _remains) = session.decode(conn, &mut buf)?;
Expand Down Expand Up @@ -170,8 +179,6 @@ fn main() -> Result<()> {
Ok(())
})?;

mount.unmount()?;

Ok(())
}

Expand Down