Skip to content

Commit

Permalink
feat: start initializing a private drive
Browse files Browse the repository at this point in the history
  • Loading branch information
sstelfox committed Feb 12, 2024
1 parent 809c891 commit d173447
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 43 deletions.
8 changes: 7 additions & 1 deletion src/codec/actor_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::codec::crypto::Fingerprint;
use crate::codec::AsyncEncodable;

// todo(sstelfox) likely need a vector clock here...
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ActorId(Fingerprint);

impl ActorId {
Expand All @@ -15,6 +15,12 @@ impl ActorId {
}
}

impl From<Fingerprint> for ActorId {
fn from(fingerprint: Fingerprint) -> Self {
Self(fingerprint)
}
}

#[async_trait]
impl AsyncEncodable for ActorId {
async fn encode<W: AsyncWrite + Unpin + Send>(
Expand Down
2 changes: 1 addition & 1 deletion src/codec/crypto/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::codec::AsyncEncodable;

const FINGERPRINT_SIZE: usize = 32;

#[derive(Clone, Copy, PartialEq)]
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct Fingerprint([u8; FINGERPRINT_SIZE]);

impl Fingerprint {
Expand Down
8 changes: 4 additions & 4 deletions src/codec/filesystem/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nom::number::streaming::{le_u64, le_u8};
use nom::IResult;
use time::OffsetDateTime;

use crate::codec::filesystem::Permissions;
use crate::codec::filesystem::FilesystemPermissions;
use crate::codec::ActorId;
use crate::codec::AsyncEncodable;

Expand All @@ -25,7 +25,7 @@ const ATTRIBUTE_CUSTOM_TYPE_ID: u8 = 0xff;

pub enum Attribute {
Owner(ActorId),
Permissions(Permissions),
Permissions(FilesystemPermissions),

CreatedAt(OffsetDateTime),
ModifiedAt(OffsetDateTime),
Expand Down Expand Up @@ -60,8 +60,8 @@ impl Attribute {
(remaining, Self::Owner(actor_id))
}
ATTRIBUTE_PERMISSIONS_TYPE_ID => {
let (remaining, permissions) = Permissions::parse(remaining)?;
(remaining, Self::Permissions(permissions))
let (remaining, fs_perms) = FilesystemPermissions::parse(remaining)?;
(remaining, Self::Permissions(fs_perms))
}
ATTRIBUTE_CREATED_AT_TYPE_ID => {
let (remaining, unix_milliseconds) = le_u64(remaining)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const PERMISSIONS_IMMUTABLE: u8 = 0b0000_0010;
const PERMISSIONS_CREATOR_WRITE_ONLY: u8 = 0b0000_0001;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Permissions {
pub struct FilesystemPermissions {
creator_write_only: bool,
executable: bool,
immutable: bool,
}

impl Permissions {
impl FilesystemPermissions {
pub fn creator_write_only(&self) -> bool {
self.creator_write_only
}
Expand All @@ -36,7 +36,7 @@ impl Permissions {
}
}

impl Permissions {
impl FilesystemPermissions {
pub fn parse(input: &[u8]) -> IResult<&[u8], Self> {
let (input, byte) = le_u8(input)?;

Expand All @@ -48,7 +48,7 @@ impl Permissions {
let executable = byte & PERMISSIONS_EXECUTABLE != 0;
let immutable = byte & PERMISSIONS_IMMUTABLE != 0;

let permissions = Permissions {
let permissions = Self {
creator_write_only,
executable,
immutable,
Expand All @@ -59,11 +59,11 @@ impl Permissions {
}

#[async_trait]
impl AsyncEncodable for Permissions {
impl AsyncEncodable for FilesystemPermissions {
async fn encode<W: AsyncWrite + Unpin + Send>(
&self,
writer: &mut W,
pos: usize,
_pos: usize,
) -> std::io::Result<usize> {
let mut options: u8 = 0x00;

Expand All @@ -81,6 +81,6 @@ impl AsyncEncodable for Permissions {

writer.write_all(&[options]).await?;

Ok(pos + 1)
Ok(1)
}
}
5 changes: 3 additions & 2 deletions src/codec/filesystem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod attribute;
mod filesystem_permissions;
mod node_type;
mod permissions;

pub use attribute::Attribute;
pub use filesystem_permissions::FilesystemPermissions;
pub use node_type::NodeType;
pub use permissions::Permissions;

49 changes: 25 additions & 24 deletions src/filesystem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,42 @@ pub use content_reference::ContentReference;
pub use file_content::FileContent;
pub use nodes::*;

use crate::codec::crypto::SigningKey;
use crate::codec::FilesystemId;
use std::collections::HashMap;

use crate::codec::content_payload::KeyAccessSettings;
use crate::codec::crypto::{SigningKey, VerifyingKey};
use crate::codec::{ActorId, FilesystemId};

pub struct Drive {
_filesystem_id: FilesystemId,
_root: DriveDirectory,
_keys: HashMap<ActorId, (VerifyingKey, KeyAccessSettings)>,
}

impl Drive {
pub fn initialize(_signing_key: &SigningKey) -> Self {
pub fn initialize_private(signing_key: &SigningKey) -> Self {
let mut rng = crate::utils::crypto_rng();

Self {
_filesystem_id: FilesystemId::generate(&mut rng),
_root: DriveDirectory::new(),
}
}
}
let verifying_key = signing_key.verifying_key();
let fingerprint = signing_key.fingerprint();
let actor_id = ActorId::from(fingerprint);

#[derive(Clone)]
pub enum DriveEntity {
File(DriveFile),
Directory(DriveDirectory),
}
let kas = KeyAccessSettings::Private {
protected: true,
owner: true,
historical: false,

#[derive(Clone)]
pub struct DriveFile {
_content_ref: u16,
}
realized_key_present: true,
data_key_present: true,
journal_key_present: true,
maintenance_key_present: true,
};

#[derive(Clone)]
pub struct DriveDirectory;
let mut keys = HashMap::new();
keys.insert(actor_id, (verifying_key, kas));

impl DriveDirectory {
pub(crate) fn new() -> Self {
Self
Self {
_filesystem_id: FilesystemId::generate(&mut rng),
_keys: keys,
}
}
}
6 changes: 3 additions & 3 deletions src/filesystem/nodes/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nom::error::ErrorKind;
use nom::number::streaming::le_u8;
use time::OffsetDateTime;

use crate::codec::filesystem::{Attribute, Permissions};
use crate::codec::filesystem::{Attribute, FilesystemPermissions};
use crate::codec::{ActorId, AsyncEncodable, Cid};
use crate::filesystem::FileContent;

Expand All @@ -16,7 +16,7 @@ const MIME_TYPE_KEY: &str = "mime_type";
pub struct File {
owner: ActorId,

permissions: Permissions,
permissions: FilesystemPermissions,
created_at: OffsetDateTime,
modified_at: OffsetDateTime,

Expand Down Expand Up @@ -114,7 +114,7 @@ impl File {
Ok((remaining, file))
}

pub fn permissions(&self) -> Permissions {
pub fn permissions(&self) -> FilesystemPermissions {
self.permissions
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> BanyanFsResult<()> {
tracing::info!("output_stream: {:02x?}", output_stream);

let signing_key = SigningKey::generate(&mut rng);
let _drive = Drive::initialize(&signing_key);
let _drive = Drive::initialize_private(&signing_key);

//if !drive.check_accessibility(key) {
// tracing::error!("key doesn't have access to the drive");
Expand Down

0 comments on commit d173447

Please sign in to comment.