Skip to content

Commit

Permalink
Cleanup to allow use of most functions when linking the library, incl…
Browse files Browse the repository at this point in the history
…uding mounting and creating filesystems
  • Loading branch information
jackpot51 committed Sep 26, 2017
1 parent b272cc2 commit 070aeb5
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 153 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "redoxfs"
description = "The Redox Filesystem"
repository = "https://github.com/redox-os/redoxfs"
version = "0.1.2"
version = "0.2.0"
license-file = "LICENSE"
readme = "README.md"
authors = ["Jeremy Soller <jackpot51@gmail.com>"]
Expand All @@ -13,16 +13,17 @@ path = "src/lib.rs"

[[bin]]
name = "redoxfs"
path = "mount/main.rs"
path = "src/bin/mount.rs"

[[bin]]
name = "redoxfs-mkfs"
path = "mkfs/main.rs"
path = "src/bin/mkfs.rs"

[dependencies]
spin = { git = "https://github.com/messense/spin-rs", rev = "020f1b3f" }
redox_syscall = "0.1"

[target.'cfg(unix)'.dependencies]
fuse = "0.2"
libc = "0.2"
time = "0.1"
47 changes: 0 additions & 47 deletions mkfs/image.rs

This file was deleted.

11 changes: 3 additions & 8 deletions mkfs/main.rs → src/bin/mkfs.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#![deny(warnings)]

extern crate redoxfs;
extern crate syscall;

use std::{env, process, time};

use redoxfs::FileSystem;

use image::Image;

pub mod image;
use redoxfs::{FileSystem, DiskFile};

fn main() {
let mut args = env::args();
if let Some(path) = args.nth(1) {
let ctime = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap();

//Open an existing image
match Image::open(&path) {
Ok(disk) => match FileSystem::create(Box::new(disk), ctime.as_secs(), ctime.subsec_nanos()) {
match DiskFile::open(&path) {
Ok(disk) => match FileSystem::create(disk, ctime.as_secs(), ctime.subsec_nanos()) {
Ok(filesystem) => {
println!("redoxfs-mkfs: created filesystem on {}, size {} MB", path, filesystem.header.1.size/1024/1024);
},
Expand Down
50 changes: 3 additions & 47 deletions mount/main.rs → src/bin/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,13 @@
extern crate libc;

extern crate redoxfs;
extern crate syscall;

use std::env;
use std::fs::File;
use std::os::unix::io::FromRawFd;
use std::path::Path;
use std::process;

use cache::Cache;
use image::Image;

pub mod cache;
pub mod image;

#[cfg(unix)]
pub mod fuse;

#[cfg(target_os = "redox")]
pub mod redox;
use redoxfs::{DiskCache, DiskFile, mount};

#[cfg(unix)]
fn fork() -> isize {
Expand All @@ -35,38 +23,6 @@ fn pipe(pipes: &mut [i32; 2]) -> isize {
unsafe { libc::pipe(pipes.as_mut_ptr()) as isize }
}

#[cfg(all(unix, target_os = "macos"))]
fn mount<P: AsRef<Path>>(filesystem: redoxfs::FileSystem, mountpoint: &P, mut write: File) {
use std::ffi::OsStr;
use std::io::Write;

let _ = write.write(&[0]);
drop(write);

fuse::mount(fuse::Fuse {
fs: filesystem
}, mountpoint, &[
// One of the uses of this redoxfs fuse wrapper is to populate a filesystem
// while building the Redox OS kernel. This means that we need to write on
// a filesystem that belongs to `root`, which in turn means that we need to
// be `root`, thus that we need to allow `root` to have access.
OsStr::new("-o"),
OsStr::new("defer_permissions"),
]);
}

#[cfg(all(unix, not(target_os = "macos")))]
fn mount<P: AsRef<Path>>(filesystem: redoxfs::FileSystem, mountpoint: &P, mut write: File) {
use std::io::Write;

let _ = write.write(&[0]);
drop(write);

fuse::mount(fuse::Fuse {
fs: filesystem
}, mountpoint, &[]);
}

#[cfg(target_os = "redox")]
fn fork() -> isize {
unsafe { syscall::Error::mux(syscall::clone(0)) as isize }
Expand Down Expand Up @@ -100,8 +56,8 @@ fn main() {

if let Some(path) = env::args().nth(1) {
//Open an existing image
match Image::open(&path).map(|image| Cache::new(image)) {
Ok(disk) => match redoxfs::FileSystem::open(Box::new(disk)) {
match DiskFile::open(&path).map(|image| DiskCache::new(image)) {
Ok(disk) => match redoxfs::FileSystem::open(disk) {
Ok(filesystem) => {
println!("redoxfs: opened filesystem {}", path);

Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 6 additions & 7 deletions mount/cache/mod.rs → src/disk/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{cmp, ptr};

use redoxfs::Disk;

use syscall::error::Result;

use disk::Disk;

use self::lru_cache::LruCache;

mod linked_hash_map;
Expand All @@ -15,21 +14,21 @@ fn copy_memory(src: &[u8], dest: &mut [u8]) -> usize {
len
}

pub struct Cache<T> {
pub struct DiskCache<T> {
inner: T,
cache: LruCache<u64, [u8; 512]>,
}

impl<T: Disk> Cache<T> {
impl<T: Disk> DiskCache<T> {
pub fn new(inner: T) -> Self {
Cache {
DiskCache {
inner: inner,
cache: LruCache::new(65536) // 32 MB cache
}
}
}

impl<T: Disk> Disk for Cache<T> {
impl<T: Disk> Disk for DiskCache<T> {
fn read_at(&mut self, block: u64, buffer: &mut [u8]) -> Result<usize> {
// println!("Cache read at {}", block);

Expand Down
20 changes: 10 additions & 10 deletions mount/image.rs → src/disk/file.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Seek, SeekFrom};

use redoxfs::Disk;
use syscall::error::{Error, Result, EIO};

use disk::Disk;

macro_rules! try_disk {
($expr:expr) => (match $expr {
Ok(val) => val,
Err(err) => {
println!("Disk I/O Error: {}", err);
eprintln!("Disk I/O Error: {}", err);
return Err(Error::new(EIO));
}
})
}

pub struct Image {
pub struct DiskFile {
file: File
}

impl Image {
pub fn open(path: &str) -> Result<Image> {
impl DiskFile {
pub fn open(path: &str) -> Result<DiskFile> {
let file = try_disk!(OpenOptions::new().read(true).write(true).open(path));
Ok(Image {
Ok(DiskFile {
file: file
})
}

pub fn create(path: &str, size: u64) -> Result<Image> {
pub fn create(path: &str, size: u64) -> Result<DiskFile> {
let file = try_disk!(OpenOptions::new().read(true).write(true).create(true).open(path));
try_disk!(file.set_len(size));
Ok(Image {
Ok(DiskFile {
file: file
})
}
}

impl Disk for Image {
impl Disk for DiskFile {
fn read_at(&mut self, block: u64, buffer: &mut [u8]) -> Result<usize> {
try_disk!(self.file.seek(SeekFrom::Start(block * 512)));
let count = try_disk!(self.file.read(buffer));
Expand Down
6 changes: 6 additions & 0 deletions src/disk.rs → src/disk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use syscall::error::Result;

pub use self::cache::DiskCache;
pub use self::file::DiskFile;

mod cache;
mod file;

/// A disk
pub trait Disk {
fn read_at(&mut self, block: u64, buffer: &mut [u8]) -> Result<usize>;
Expand Down
10 changes: 5 additions & 5 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use syscall::error::{Result, Error, EEXIST, EISDIR, ENOENT, ENOSPC, ENOTDIR, ENO
use super::{Disk, ExNode, Extent, Header, Node};

/// A file system
pub struct FileSystem {
pub disk: Box<Disk>,
pub struct FileSystem<D: Disk> {
pub disk: D,
pub block: u64,
pub header: (u64, Header)
}

impl FileSystem {
impl<D: Disk> FileSystem<D> {
/// Open a file system on a disk
pub fn open(mut disk: Box<Disk>) -> Result<Self> {
pub fn open(mut disk: D) -> Result<Self> {
for block in 0..65536 {
let mut header = (0, Header::default());
disk.read_at(block + header.0, &mut header.1)?;
Expand All @@ -37,7 +37,7 @@ impl FileSystem {
}

/// Create a file system on a disk
pub fn create(mut disk: Box<Disk>, ctime: u64, ctime_nsec: u32) -> Result<Self> {
pub fn create(mut disk: D, ctime: u64, ctime_nsec: u32) -> Result<Self> {
let size = disk.size()?;

if size >= 4 * 512 {
Expand Down
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@

extern crate syscall;

pub use self::disk::Disk;
pub use self::disk::{Disk, DiskCache, DiskFile};
pub use self::ex_node::ExNode;
pub use self::extent::Extent;
pub use self::filesystem::FileSystem;
pub use self::header::Header;
pub use self::mount::mount;
pub use self::node::Node;

pub mod disk;
pub mod ex_node;
pub mod extent;
pub mod filesystem;
pub mod header;
pub mod node;
mod disk;
mod ex_node;
mod extent;
mod filesystem;
mod header;
mod mount;
mod node;
Loading

0 comments on commit 070aeb5

Please sign in to comment.