Format the codebase

parent a6af6a92
Pipeline #6870 passed with stages
in 2 minutes and 32 seconds
use std::fs;
use std::io;
use std::path::Path;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
use crate::{BLOCK_SIZE, Disk, Extent, FileSystem, Node};
use crate::{Disk, Extent, FileSystem, Node, BLOCK_SIZE};
fn syscall_err(err: syscall::Error) -> io::Error {
io::Error::from_raw_os_error(err.errno)
}
pub fn archive_at<D: Disk, P: AsRef<Path>>(fs: &mut FileSystem<D>, parent_path: P, parent_block: u64) -> io::Result<()> {
pub fn archive_at<D: Disk, P: AsRef<Path>>(
fs: &mut FileSystem<D>,
parent_path: P,
parent_block: u64,
) -> io::Result<()> {
for entry_res in fs::read_dir(parent_path)? {
let entry = entry_res?;
let metadata = entry.metadata()?;
let file_type = metadata.file_type();
let name = entry.file_name().into_string().map_err(|_|
io::Error::new(
io::ErrorKind::InvalidData,
"filename is not valid UTF-8"
)
)?;
let name = entry.file_name().into_string().map_err(|_| {
io::Error::new(io::ErrorKind::InvalidData, "filename is not valid UTF-8")
})?;
let mode_type = if file_type.is_dir() {
Node::MODE_DIR
......@@ -33,18 +34,20 @@ pub fn archive_at<D: Disk, P: AsRef<Path>>(fs: &mut FileSystem<D>, parent_path:
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Does not support parsing {:?}", file_type)
format!("Does not support parsing {:?}", file_type),
));
};
let mode = mode_type | (metadata.mode() as u16 & Node::MODE_PERM);
let mut node = fs.create_node(
mode,
&name,
parent_block,
metadata.ctime() as u64,
metadata.ctime_nsec() as u32
).map_err(syscall_err)?;
let mut node = fs
.create_node(
mode,
&name,
parent_block,
metadata.ctime() as u64,
metadata.ctime_nsec() as u32,
)
.map_err(syscall_err)?;
node.1.uid = metadata.uid();
node.1.gid = metadata.gid();
fs.write_at(node.0, &node.1).map_err(syscall_err)?;
......@@ -59,8 +62,9 @@ pub fn archive_at<D: Disk, P: AsRef<Path>>(fs: &mut FileSystem<D>, parent_path:
0,
&data,
metadata.mtime() as u64,
metadata.mtime_nsec() as u32
).map_err(syscall_err)?;
metadata.mtime_nsec() as u32,
)
.map_err(syscall_err)?;
} else if file_type.is_symlink() {
let destination = fs::read_link(path)?;
let data = destination.as_os_str().as_bytes();
......@@ -69,12 +73,13 @@ pub fn archive_at<D: Disk, P: AsRef<Path>>(fs: &mut FileSystem<D>, parent_path:
0,
&data,
metadata.mtime() as u64,
metadata.mtime_nsec() as u32
).map_err(syscall_err)?;
metadata.mtime_nsec() as u32,
)
.map_err(syscall_err)?;
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Does not support creating {:?}", file_type)
format!("Does not support creating {:?}", file_type),
));
}
}
......
......@@ -2,11 +2,11 @@ extern crate redoxfs;
extern crate syscall;
extern crate uuid;
use std::{env, fs, process};
use std::io::Read;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, fs, process};
use redoxfs::{DiskSparse, FileSystem, archive};
use redoxfs::{archive, DiskSparse, FileSystem};
use uuid::Uuid;
fn main() {
......@@ -44,12 +44,18 @@ fn main() {
Ok(mut file) => match file.read_to_end(&mut bootloader) {
Ok(_) => (),
Err(err) => {
println!("redoxfs-ar: failed to read bootloader {}: {}", bootloader_path, err);
println!(
"redoxfs-ar: failed to read bootloader {}: {}",
bootloader_path, err
);
process::exit(1);
}
},
Err(err) => {
println!("redoxfs-ar: failed to open bootloader {}: {}", bootloader_path, err);
println!(
"redoxfs-ar: failed to open bootloader {}: {}",
bootloader_path, err
);
process::exit(1);
}
}
......@@ -67,7 +73,10 @@ fn main() {
};
if let Err(err) = fs.disk.file.set_len(size) {
println!("redoxfs-ar: failed to truncate {} to {}: {}", disk_path, size, err);
println!(
"redoxfs-ar: failed to truncate {} to {}: {}",
disk_path, size, err
);
process::exit(1);
}
......@@ -76,12 +85,15 @@ fn main() {
"redoxfs-ar: created filesystem on {}, reserved {} blocks, size {} MB, uuid {}",
disk_path,
fs.block,
fs.header.1.size/1000/1000,
fs.header.1.size / 1000 / 1000,
uuid.hyphenated()
);
},
}
Err(err) => {
println!("redoxfs-ar: failed to create filesystem on {}: {}", disk_path, err);
println!(
"redoxfs-ar: failed to create filesystem on {}: {}",
disk_path, err
);
process::exit(1);
}
};
......
extern crate redoxfs;
extern crate uuid;
use std::{env, fs, process, time};
use std::io::Read;
use std::{env, fs, process, time};
use redoxfs::{FileSystem, DiskFile};
use redoxfs::{DiskFile, FileSystem};
use uuid::Uuid;
fn main() {
......@@ -34,25 +34,42 @@ fn main() {
Ok(mut file) => match file.read_to_end(&mut bootloader) {
Ok(_) => (),
Err(err) => {
println!("redoxfs-mkfs: failed to read bootloader {}: {}", bootloader_path, err);
println!(
"redoxfs-mkfs: failed to read bootloader {}: {}",
bootloader_path, err
);
process::exit(1);
}
},
Err(err) => {
println!("redoxfs-mkfs: failed to open bootloader {}: {}", bootloader_path, err);
println!(
"redoxfs-mkfs: failed to open bootloader {}: {}",
bootloader_path, err
);
process::exit(1);
}
}
};
let ctime = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap();
let ctime = time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap();
match FileSystem::create_reserved(disk, &bootloader, ctime.as_secs(), ctime.subsec_nanos()) {
Ok(filesystem) => {
let uuid = Uuid::from_bytes(&filesystem.header.1.uuid).unwrap();
println!("redoxfs-mkfs: created filesystem on {}, reserved {} blocks, size {} MB, uuid {}", disk_path, filesystem.block, filesystem.header.1.size/1000/1000, uuid.hyphenated());
},
println!(
"redoxfs-mkfs: created filesystem on {}, reserved {} blocks, size {} MB, uuid {}",
disk_path,
filesystem.block,
filesystem.header.1.size / 1000 / 1000,
uuid.hyphenated()
);
}
Err(err) => {
println!("redoxfs-mkfs: failed to create filesystem on {}: {}", disk_path, err);
println!(
"redoxfs-mkfs: failed to create filesystem on {}: {}",
disk_path, err
);
process::exit(1);
}
}
......
......@@ -15,7 +15,7 @@ use std::io::{Read, Write};
use std::os::unix::io::{FromRawFd, RawFd};
use std::process;
use redoxfs::{DiskCache, DiskFile, mount};
use redoxfs::{mount, DiskCache, DiskFile};
use uuid::Uuid;
#[cfg(target_os = "redox")]
......@@ -33,7 +33,7 @@ fn setsig() {
let sig_action = SigAction {
sa_handler: unmount_handler,
sa_mask: [0,0],
sa_mask: [0, 0],
sa_flags: 0,
};
......@@ -94,17 +94,19 @@ fn disk_paths(paths: &mut Vec<String>) {
let mut schemes = vec![];
match fs::read_dir(":") {
Ok(entries) => for entry_res in entries {
if let Ok(entry) = entry_res {
if let Ok(path) = entry.path().into_os_string().into_string() {
let scheme = path.trim_start_matches(':').trim_matches('/');
if scheme.starts_with("disk") {
println!("redoxfs: found scheme {}", scheme);
schemes.push(format!("{}:", scheme));
Ok(entries) => {
for entry_res in entries {
if let Ok(entry) = entry_res {
if let Ok(path) = entry.path().into_os_string().into_string() {
let scheme = path.trim_start_matches(':').trim_matches('/');
if scheme.starts_with("disk") {
println!("redoxfs: found scheme {}", scheme);
schemes.push(format!("{}:", scheme));
}
}
}
}
},
}
Err(err) => {
println!("redoxfs: failed to list schemes: {}", err);
}
......@@ -112,14 +114,16 @@ fn disk_paths(paths: &mut Vec<String>) {
for scheme in schemes {
match fs::read_dir(&scheme) {
Ok(entries) => for entry_res in entries {
if let Ok(entry) = entry_res {
if let Ok(path) = entry.path().into_os_string().into_string() {
println!("redoxfs: found path {}", path);
paths.push(path);
Ok(entries) => {
for entry_res in entries {
if let Ok(entry) = entry_res {
if let Ok(path) = entry.path().into_os_string().into_string() {
println!("redoxfs: found path {}", path);
paths.push(path);
}
}
}
},
}
Err(err) => {
println!("redoxfs: failed to list '{}': {}", scheme, err);
}
......@@ -136,11 +140,11 @@ fn daemon(disk_id: &DiskId, mountpoint: &str, block_opt: Option<u64>, mut write:
match *disk_id {
DiskId::Path(ref path) => {
paths.push(path.clone());
},
}
DiskId::Uuid(ref uuid) => {
disk_paths(&mut paths);
uuid_opt = Some(uuid.clone());
},
}
}
for path in paths {
......@@ -148,15 +152,28 @@ fn daemon(disk_id: &DiskId, mountpoint: &str, block_opt: Option<u64>, mut write:
match DiskFile::open(&path).map(|image| DiskCache::new(image)) {
Ok(disk) => match redoxfs::FileSystem::open(disk, block_opt) {
Ok(filesystem) => {
println!("redoxfs: opened filesystem on {} with uuid {}", path,
Uuid::from_bytes(&filesystem.header.1.uuid).unwrap().hyphenated());
println!(
"redoxfs: opened filesystem on {} with uuid {}",
path,
Uuid::from_bytes(&filesystem.header.1.uuid)
.unwrap()
.hyphenated()
);
let matches = if let Some(uuid) = uuid_opt {
if &filesystem.header.1.uuid == uuid.as_bytes() {
println!("redoxfs: filesystem on {} matches uuid {}", path, uuid.hyphenated());
println!(
"redoxfs: filesystem on {} matches uuid {}",
path,
uuid.hyphenated()
);
true
} else {
println!("redoxfs: filesystem on {} does not match uuid {}", path, uuid.hyphenated());
println!(
"redoxfs: filesystem on {} does not match uuid {}",
path,
uuid.hyphenated()
);
false
}
} else {
......@@ -167,31 +184,38 @@ fn daemon(disk_id: &DiskId, mountpoint: &str, block_opt: Option<u64>, mut write:
match mount(filesystem, &mountpoint, |mounted_path| {
capability_mode();
println!("redoxfs: mounted filesystem on {} to {}", path, mounted_path.display());
println!(
"redoxfs: mounted filesystem on {} to {}",
path,
mounted_path.display()
);
let _ = write.write(&[0]);
}) {
Ok(()) => {
process::exit(0);
},
}
Err(err) => {
println!("redoxfs: failed to mount {} to {}: {}", path, mountpoint, err);
println!(
"redoxfs: failed to mount {} to {}: {}",
path, mountpoint, err
);
}
}
}
},
Err(err) => println!("redoxfs: failed to open filesystem {}: {}", path, err)
}
Err(err) => println!("redoxfs: failed to open filesystem {}: {}", path, err),
},
Err(err) => println!("redoxfs: failed to open image {}: {}", path, err)
Err(err) => println!("redoxfs: failed to open image {}: {}", path, err),
}
}
match *disk_id {
DiskId::Path(ref path) => {
println!("redoxfs: not able to mount path {}", path);
},
}
DiskId::Uuid(ref uuid) => {
println!("redoxfs: not able to mount uuid {}", uuid.hyphenated());
},
}
}
let _ = write.write(&[1]);
......@@ -202,11 +226,16 @@ fn print_uuid(path: &str) {
match DiskFile::open(&path).map(|image| DiskCache::new(image)) {
Ok(disk) => match redoxfs::FileSystem::open(disk, None) {
Ok(filesystem) => {
println!("{}", Uuid::from_bytes(&filesystem.header.1.uuid).unwrap().hyphenated());
},
Err(err) => println!("redoxfs: failed to open filesystem {}: {}", path, err)
println!(
"{}",
Uuid::from_bytes(&filesystem.header.1.uuid)
.unwrap()
.hyphenated()
);
}
Err(err) => println!("redoxfs: failed to open filesystem {}: {}", path, err),
},
Err(err) => println!("redoxfs: failed to open image {}: {}", path, err)
Err(err) => println!("redoxfs: failed to open image {}: {}", path, err),
}
}
......@@ -214,39 +243,41 @@ fn main() {
let mut args = env::args().skip(1);
let disk_id = match args.next() {
Some(arg) => if arg == "--uuid" {
let uuid = match args.next() {
Some(arg) => match Uuid::parse_str(&arg) {
Ok(uuid) => uuid,
Err(err) => {
println!("redoxfs: invalid uuid '{}': {}", arg, err);
Some(arg) => {
if arg == "--uuid" {
let uuid = match args.next() {
Some(arg) => match Uuid::parse_str(&arg) {
Ok(uuid) => uuid,
Err(err) => {
println!("redoxfs: invalid uuid '{}': {}", arg, err);
usage();
process::exit(1);
}
},
None => {
println!("redoxfs: no uuid provided");
usage();
process::exit(1);
}
},
None => {
println!("redoxfs: no uuid provided");
usage();
process::exit(1);
}
};
DiskId::Uuid(uuid)
} else if arg == "--get-uuid" {
match args.next() {
Some(arg) => {
print_uuid(&arg);
process::exit(1);
},
None => {
println!("redoxfs: no disk provided");
usage();
process::exit(1);
},
};
} else {
DiskId::Path(arg)
},
};
DiskId::Uuid(uuid)
} else if arg == "--get-uuid" {
match args.next() {
Some(arg) => {
print_uuid(&arg);
process::exit(1);
}
None => {
println!("redoxfs: no disk provided");
usage();
process::exit(1);
}
};
} else {
DiskId::Path(arg)
}
}
None => {
println!("redoxfs: no disk provided");
usage();
......
use std::{cmp, ptr};
use std::collections::{HashMap, VecDeque};
use std::{cmp, ptr};
use syscall::error::Result;
use BLOCK_SIZE;
use disk::Disk;
use BLOCK_SIZE;
fn copy_memory(src: &[u8], dest: &mut [u8]) -> usize {
let len = cmp::min(src.len(), dest.len());
......@@ -45,16 +45,16 @@ impl<T: Disk> Disk for DiskCache<T> {
let mut read = 0;
let mut failed = false;
for i in 0..(buffer.len() + BLOCK_SIZE as usize - 1)/(BLOCK_SIZE as usize) {
for i in 0..(buffer.len() + BLOCK_SIZE as usize - 1) / (BLOCK_SIZE as usize) {
let block_i = block + i as u64;
let buffer_i = i * BLOCK_SIZE as usize;
let buffer_j = cmp::min(buffer_i + BLOCK_SIZE as usize, buffer.len());
let buffer_slice = &mut buffer[buffer_i .. buffer_j];
let buffer_slice = &mut buffer[buffer_i..buffer_j];
if let Some(cache_buf) = self.cache.get_mut(&block_i) {
read += copy_memory(cache_buf, buffer_slice);
}else{
} else {
failed = true;
break;
}
......@@ -64,12 +64,12 @@ impl<T: Disk> Disk for DiskCache<T> {
self.inner.read_at(block, buffer)?;
read = 0;
for i in 0..(buffer.len() + BLOCK_SIZE as usize - 1)/(BLOCK_SIZE as usize) {
for i in 0..(buffer.len() + BLOCK_SIZE as usize - 1) / (BLOCK_SIZE as usize) {
let block_i = block + i as u64;
let buffer_i = i * BLOCK_SIZE as usize;
let buffer_j = cmp::min(buffer_i + BLOCK_SIZE as usize, buffer.len());
let buffer_slice = &buffer[buffer_i .. buffer_j];
let buffer_slice = &buffer[buffer_i..buffer_j];
let mut cache_buf = [0; BLOCK_SIZE as usize];
read += copy_memory(buffer_slice, &mut cache_buf);
......@@ -87,12 +87,12 @@ impl<T: Disk> Disk for DiskCache<T> {
self.inner.write_at(block, buffer)?;
let mut written = 0;
for i in 0..(buffer.len() + BLOCK_SIZE as usize - 1)/(BLOCK_SIZE as usize) {
for i in 0..(buffer.len() + BLOCK_SIZE as usize - 1) / (BLOCK_SIZE as usize) {
let block_i = block + i as u64;
let buffer_i = i * BLOCK_SIZE as usize;
let buffer_j = cmp::min(buffer_i + BLOCK_SIZE as usize, buffer.len());
let buffer_slice = &buffer[buffer_i .. buffer_j];
let buffer_slice = &buffer[buffer_i..buffer_j];
let mut cache_buf = [0; BLOCK_SIZE as usize];
written += copy_memory(buffer_slice, &mut cache_buf);
......
use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Seek, SeekFrom};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::Path;
use syscall::error::{Error, Result, EIO};
use BLOCK_SIZE;
use disk::Disk;
use BLOCK_SIZE;
macro_rules! try_disk {
($expr:expr) => (match $expr {
Ok(val) => val,
Err(err) => {
eprintln!("Disk I/O Error: {}", err);
return Err(Error::new(EIO));
($expr:expr) => {
match $expr {
Ok(val) => val,
Err(err) => {
eprintln!("Disk I/O Error: {}", err);
return Err(Error::new(EIO));
}
}
})
};
}
pub struct DiskFile {
pub file: File
pub file: File,
}
impl DiskFile {
pub fn open<P: AsRef<Path>>(path: P) -> Result<DiskFile> {
let file = try_disk!(OpenOptions::new().read(true).write(true).open(path));
Ok(DiskFile {
file: file
})
Ok(DiskFile { file: file })
}
pub fn create<P: AsRef<Path>>(path: P, size: u64) -> Result<DiskFile> {
let file = try_disk!(OpenOptions::new().read(true).write(true).create(true).open(path));
let file = try_disk!(OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(path));
try_disk!(file.set_len(size));
Ok(DiskFile {
file: file
})
Ok(DiskFile { file: file })
}
}
......
use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Seek, SeekFrom};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::u64;
use syscall::error::{Error, Result, EIO};
use BLOCK_SIZE;
use disk::Disk;
use BLOCK_SIZE;
macro_rules! try_disk {
($expr:expr) => (match $expr {
Ok(val) => val,
Err(err) => {
eprintln!("Disk I/O Error: {}", err);
return Err(Error::new(EIO));
($expr:expr) => {
<