rename block to sector to better reflect its purpose

This commit is contained in:
Szymon Walter 2018-03-20 12:46:27 +01:00
parent 6e86749246
commit ab069b60e6
7 changed files with 51 additions and 51 deletions

View file

@ -7,7 +7,7 @@ use alloc::boxed::Box;
use alloc::borrow::{Cow, ToOwned};
use error::Infallible;
use block::{Address, Size};
use sector::{Address, Size};
pub mod length;
use self::length::Length;
@ -283,7 +283,7 @@ mod file {
use std::fs::File;
use std::cell::RefCell;
use block::{Address, Size};
use sector::{Address, Size};
use super::{Buffer, BufferCommit, BufferSlice};
use super::length::Length;
@ -355,7 +355,7 @@ mod file {
#[cfg(test)]
mod tests {
use block::{Address, Size512};
use sector::{Address, Size512};
use super::*;
#[test]

View file

@ -4,7 +4,7 @@ use core::marker::PhantomData;
use alloc::Vec;
use error::Error;
use block::{Address, Size};
use sector::{Address, Size};
use buffer::{Buffer, BufferSlice};
use sys::superblock::Superblock;
use sys::block_group::BlockGroupDescriptor;
@ -221,7 +221,7 @@ mod tests {
use std::fs::File;
use std::cell::RefCell;
use block::{Address, Size512};
use sector::{Address, Size512};
use buffer::Buffer;
use super::Ext2;

View file

@ -15,7 +15,7 @@ extern crate core;
pub mod error;
pub mod sys;
pub mod block;
pub mod sector;
pub mod buffer;
pub mod fs;

View file

@ -5,7 +5,7 @@ use core::fmt::{self, Debug, Display, LowerHex};
use core::iter::Step;
pub trait Size: PartialOrd {
// log_block_size = log_2(block_size)
// log_sector_size = log_2(sector_size)
const LOG_SIZE: u32;
const SIZE: usize = 1 << Self::LOG_SIZE;
const OFFSET_MASK: usize = Self::SIZE - 1;
@ -32,26 +32,26 @@ impl Size for Size4096 {
/// Address in a physical sector
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Address<S: Size> {
block: usize,
sector: usize,
offset: usize,
_phantom: PhantomData<S>,
}
impl<S: Size> Address<S> {
pub unsafe fn new_unchecked(block: usize, offset: usize) -> Address<S> {
assert!(offset < S::SIZE, "offset out of block bounds");
pub unsafe fn new_unchecked(sector: usize, offset: usize) -> Address<S> {
assert!(offset < S::SIZE, "offset out of sector bounds");
let _phantom = PhantomData;
Address {
block,
sector,
offset,
_phantom,
}
}
pub fn new(block: usize, offset: isize) -> Address<S> {
let block = (block as isize + (offset >> S::LOG_SIZE)) as usize;
pub fn new(sector: usize, offset: isize) -> Address<S> {
let sector = (sector as isize + (offset >> S::LOG_SIZE)) as usize;
let offset = offset.abs() as usize & S::OFFSET_MASK;
unsafe { Address::new_unchecked(block, offset) }
unsafe { Address::new_unchecked(sector, offset) }
}
pub fn with_block_size(
@ -62,30 +62,30 @@ impl<S: Size> Address<S> {
let log_diff = log_block_size as isize - S::LOG_SIZE as isize;
let top_offset = offset >> S::LOG_SIZE;
let offset = offset & ((1 << log_block_size) - 1);
let block = block << log_diff | top_offset;
Address::new(block, offset as isize)
let sector = block << log_diff | top_offset;
Address::new(sector, offset as isize)
}
pub fn index64(&self) -> u64 {
((self.block as u64) << S::LOG_SIZE) + self.offset as u64
((self.sector as u64) << S::LOG_SIZE) + self.offset as u64
}
pub fn into_index(&self) -> Option<usize> {
self.block
self.sector
.checked_shl(S::LOG_SIZE)
.and_then(|block| block.checked_add(self.offset))
.and_then(|sector| sector.checked_add(self.offset))
}
pub const fn block_size(&self) -> usize {
pub const fn sector_size(&self) -> usize {
S::SIZE
}
pub const fn log_block_size(&self) -> u32 {
pub const fn log_sector_size(&self) -> u32 {
S::LOG_SIZE
}
pub fn block(&self) -> usize {
self.block
pub fn sector(&self) -> usize {
self.sector
}
pub fn offset(&self) -> usize {
@ -95,8 +95,8 @@ impl<S: Size> Address<S> {
impl<S: Size + Clone + PartialOrd> Step for Address<S> {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
if end.block >= start.block {
Some(end.block - start.block)
if end.sector >= start.sector {
Some(end.sector - start.sector)
} else {
None
}
@ -111,17 +111,17 @@ impl<S: Size + Clone + PartialOrd> Step for Address<S> {
}
fn add_one(&self) -> Self {
Address::new(self.block + 1, 0)
Address::new(self.sector + 1, 0)
}
fn sub_one(&self) -> Self {
Address::new(self.block - 1, 0)
Address::new(self.sector - 1, 0)
}
fn add_usize(&self, n: usize) -> Option<Self> {
self.block
self.sector
.checked_add(n)
.map(|block| Address::new(block, 0))
.map(|sector| Address::new(sector, 0))
}
}
@ -129,7 +129,7 @@ impl<S: Size> Debug for Address<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = format!("Address<{}>", S::SIZE);
f.debug_struct(&name)
.field("block", &self.block)
.field("sector", &self.sector)
.field("offset", &self.offset)
.finish()
}
@ -137,29 +137,29 @@ impl<S: Size> Debug for Address<S> {
impl<S: Size> Display for Address<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.block, self.offset)
write!(f, "{}:{}", self.sector, self.offset)
}
}
impl<S: Size> LowerHex for Address<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}:{:x}", self.block, self.offset)
write!(f, "{:x}:{:x}", self.sector, self.offset)
}
}
impl<S: Size> From<u64> for Address<S> {
fn from(idx: u64) -> Address<S> {
let block = idx >> S::LOG_SIZE;
let sector = idx >> S::LOG_SIZE;
let offset = idx & S::OFFSET_MASK as u64;
Address::new(block as usize, offset as isize)
Address::new(sector as usize, offset as isize)
}
}
impl<S: Size> From<usize> for Address<S> {
fn from(idx: usize) -> Address<S> {
let block = idx >> S::LOG_SIZE;
let sector = idx >> S::LOG_SIZE;
let offset = idx & S::OFFSET_MASK;
Address::new(block, offset as isize)
Address::new(sector, offset as isize)
}
}
@ -167,7 +167,7 @@ impl<S: Size> Add for Address<S> {
type Output = Address<S>;
fn add(self, rhs: Address<S>) -> Address<S> {
Address::new(
self.block + rhs.block,
self.sector + rhs.sector,
(self.offset + rhs.offset) as isize,
)
}
@ -177,7 +177,7 @@ impl<S: Size> Sub for Address<S> {
type Output = Address<S>;
fn sub(self, rhs: Address<S>) -> Address<S> {
Address::new(
self.block - rhs.block,
self.sector - rhs.sector,
self.offset as isize - rhs.offset as isize,
)
}

View file

@ -4,7 +4,7 @@ use core::fmt::{self, Debug};
use alloc::Vec;
use error::Error;
use block::{Address, Size};
use sector::{Address, Size};
use buffer::Buffer;
/// The Block Group Descriptor Table contains a descriptor for each block group
@ -66,9 +66,9 @@ impl BlockGroupDescriptor {
offset + Address::from(mem::size_of::<BlockGroupDescriptor>());
if haystack.len() < end {
return Err(Error::AddressOutOfBounds(
end.block(),
end.sector(),
end.offset(),
end.block_size(),
end.sector_size(),
));
}
@ -94,9 +94,9 @@ impl BlockGroupDescriptor {
+ Address::from(count * mem::size_of::<BlockGroupDescriptor>());
if haystack.len() < end {
return Err(Error::AddressOutOfBounds(
end.block(),
end.sector(),
end.offset(),
end.block_size(),
end.sector_size(),
));
}
@ -115,7 +115,7 @@ impl BlockGroupDescriptor {
#[cfg(test)]
mod tests {
use block::{Address, Size512};
use sector::{Address, Size512};
use super::*;
#[test]

View file

@ -2,7 +2,7 @@ use core::mem;
use core::fmt::{self, Debug};
use error::Error;
use block::{Address, Size};
use sector::{Address, Size};
use buffer::Buffer;
/// An inode is a structure on the disk that represents a file, directory,
@ -115,9 +115,9 @@ impl Inode {
let end = offset + Address::from(size);
if haystack.len() < end {
return Err(Error::AddressOutOfBounds(
end.block(),
end.sector(),
end.offset(),
end.block_size(),
end.sector_size(),
));
}

View file

@ -2,7 +2,7 @@ use core::mem;
use core::fmt::{self, Debug};
use error::Error;
use block::{Address, Size};
use sector::{Address, Size};
use buffer::Buffer;
/// Ext2 signature (0xef53), used to help confirm the presence of Ext2 on a
@ -205,9 +205,9 @@ impl Superblock {
let end = offset + Address::from(mem::size_of::<Superblock>());
if haystack.len() < end {
return Err(Error::AddressOutOfBounds(
end.block(),
end.sector(),
end.offset(),
end.block_size(),
end.sector_size(),
));
}
@ -296,7 +296,7 @@ bitflags! {
#[cfg(test)]
mod tests {
use block::Size512;
use sector::Size512;
use super::*;
#[test]