Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Directory support #16

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ where
}

let meta_size =
ObjectHeader::byte_count::<M>() + 4 + (blocks + 1) * M::object_location_bytes();
ObjectHeader::byte_count::<M>() + 8 + (blocks + 1) * M::object_location_bytes();

match self
.blocks
Expand Down Expand Up @@ -770,6 +770,12 @@ impl ObjectMover for DataObject {
&old_object_reader.path_hash.to_le_bytes(),
)
.await?;
meta_writer
.write(
&mut storage.medium,
&old_object_reader.directory_hash.to_le_bytes(),
)
.await?;

let filename_location = if old_object_reader.filename_location == object.location() {
destination
Expand Down
20 changes: 16 additions & 4 deletions src/ll/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use crate::{
pub enum ObjectType {
FileMetadata = 0x8F,
FileData = 0x8E,
DirectoryMetadata = 0x8D,
}

impl ObjectType {
fn parse(byte: u8) -> Option<Self> {
match byte {
v if v == Self::FileMetadata as u8 => Some(Self::FileMetadata),
v if v == Self::FileData as u8 => Some(Self::FileData),
v if v == Self::DirectoryMetadata as u8 => Some(Self::DirectoryMetadata),
_ => None,
}
}
Expand Down Expand Up @@ -431,9 +433,9 @@ impl ObjectHeader {
pub struct MetadataObjectHeader<M: StorageMedium> {
pub object: ObjectHeader,
pub path_hash: u32,
pub directory_hash: u32,
pub filename_location: ObjectLocation,
data_object_cursor: usize, // Used to iterate through the list of object locations.
_parent: Option<ObjectLocation>,
_medium: PhantomData<M>,
}

Expand All @@ -444,7 +446,7 @@ impl<M: StorageMedium> MetadataObjectHeader<M> {
) -> Result<Option<ObjectLocation>, StorageError> {
log::trace!("MetadataObjectHeader::next_object_location()");

let data_offset = 4 + M::object_location_bytes(); // path hash + filename location
let data_offset = 4 + 4 + M::object_location_bytes(); // path hash + folder hash + filename location
if self.data_object_cursor
>= self
.object
Expand Down Expand Up @@ -774,23 +776,33 @@ impl<M: StorageMedium> ObjectInfo<M> {
)
.await?;

let mut directory_hash_bytes = [0; 4];
let directory_hash_offset = path_hash_offset + 4;
medium
.read(
self.location().block,
directory_hash_offset,
&mut directory_hash_bytes,
)
.await?;

let mut filename_location_bytes = [0; 8];
let filename_location_bytes = &mut filename_location_bytes[0..M::object_location_bytes()];

medium
.read(
self.location().block,
path_hash_offset + 4,
directory_hash_offset + 4,
filename_location_bytes,
)
.await?;

Ok(MetadataObjectHeader {
object: self.header,
path_hash: u32::from_le_bytes(path_hash_bytes),
directory_hash: u32::from_le_bytes(directory_hash_bytes),
filename_location: ObjectLocation::from_bytes::<M>(filename_location_bytes),
data_object_cursor: 0,
_parent: None,
_medium: PhantomData,
})
}
Expand Down
5 changes: 5 additions & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ where
) -> Result<(), StorageError> {
log::debug!("Writer::create(path = {:?})", path);

// TODO: lift this? we need to look up the directory
if path.contains(&['/', '\\'][..]) {
log::warn!("Path contains invalid characters");
return Err(StorageError::InvalidOperation);
}

let path_hash = hash_path(path);
let directory_hash = hash_path(""); // TODO
let est_page_count = 1 + storage.estimate_data_chunks(op.estimate_length())?;

// this is mutable because we can fail mid-writing. 4 bytes to store the path hash
Expand Down Expand Up @@ -199,6 +201,9 @@ where
meta_writer
.write(&mut storage.medium, &path_hash.to_le_bytes())
.await?;
meta_writer
.write(&mut storage.medium, &directory_hash.to_le_bytes())
.await?;

storage
.write_location(&mut meta_writer, filename_location)
Expand Down