1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
//! Public node system in-memory representation.
use super::PublicNodeSerializable;
use crate::{
error::FsError,
public::{PublicDirectory, PublicFile},
traits::Id,
};
use anyhow::{bail, Result};
use async_once_cell::OnceCell;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use libipld_core::cid::Cid;
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
use std::{collections::BTreeSet, rc::Rc};
use wnfs_common::{AsyncSerialize, BlockStore, RemembersCid};
//--------------------------------------------------------------------------------------------------
// Type Definitions
//--------------------------------------------------------------------------------------------------
/// A node in the WNFS public file system. This can either be a file or a directory.
///
/// # Examples
///
/// ```
/// use wnfs::public::{PublicDirectory, PublicNode};
/// use chrono::Utc;
///
/// let dir = PublicDirectory::new_rc(Utc::now());
/// let node = PublicNode::Dir(dir);
///
/// println!("Node: {:?}", node);
/// ```
#[derive(Debug, Clone)]
pub enum PublicNode {
File(Rc<PublicFile>),
Dir(Rc<PublicDirectory>),
}
//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------
impl PublicNode {
/// Creates node with upserted modified time.
///
/// # Examples
///
/// ```
/// use wnfs::public::{PublicDirectory, PublicNode};
/// use chrono::{Utc, Duration, TimeZone};
///
/// let dir = PublicDirectory::new_rc(Utc::now());
/// let node = &mut PublicNode::Dir(dir);
///
/// let time = Utc::now();
/// node.upsert_mtime(time);
///
/// let imprecise_time = Utc.timestamp_opt(time.timestamp(), 0).single();
/// assert_eq!(
/// imprecise_time,
/// node.as_dir()
/// .unwrap()
/// .get_metadata()
/// .get_modified()
/// );
/// ```
pub fn upsert_mtime(&mut self, time: DateTime<Utc>) {
match self {
Self::File(file) => {
Rc::make_mut(file).metadata.upsert_mtime(time);
}
Self::Dir(dir) => {
Rc::make_mut(dir).metadata.upsert_mtime(time);
}
}
}
/// Creates node with updated previous pointer value.
///
/// # Examples
///
/// ```
/// use wnfs::public::{PublicDirectory, PublicNode};
/// use chrono::Utc;
/// use libipld_core::cid::Cid;
/// use std::{rc::Rc, collections::BTreeSet};
///
/// let dir = PublicDirectory::new_rc(Utc::now());
/// let node = PublicNode::Dir(dir);
///
/// let new_cids = [Cid::default()];
/// let node = node.update_previous(new_cids.to_vec());
///
/// assert_eq!(
/// &BTreeSet::from(new_cids),
/// node.as_dir()
/// .unwrap()
/// .get_previous()
/// );
/// ```
pub fn update_previous(&self, cids: Vec<Cid>) -> Self {
match self {
Self::File(file) => {
let mut file = (**file).clone();
file.previous = cids.into_iter().collect();
Self::File(Rc::new(file))
}
Self::Dir(dir) => {
let mut dir = (**dir).clone();
dir.previous = cids.into_iter().collect();
Self::Dir(Rc::new(dir))
}
}
}
/// Gets previous ancestor of a node.
///
/// # Examples
///
/// ```
/// use wnfs::public::{PublicDirectory, PublicNode};
/// use chrono::Utc;
///
/// let dir = PublicDirectory::new_rc(Utc::now());
/// let node = PublicNode::Dir(dir);
///
/// assert_eq!(
/// node.get_previous(),
/// node.as_dir()
/// .unwrap()
/// .get_previous()
/// );
/// ```
pub fn get_previous(&self) -> &BTreeSet<Cid> {
match self {
Self::File(file) => file.get_previous(),
Self::Dir(dir) => dir.get_previous(),
}
}
/// Casts a node to a directory.
///
/// # Examples
///
/// ```
/// use std::rc::Rc;
/// use wnfs::public::{PublicDirectory, PublicNode};
/// use chrono::Utc;
///
/// let dir = PublicDirectory::new_rc(Utc::now());
/// let node = PublicNode::Dir(Rc::clone(&dir));
///
/// assert_eq!(node.as_dir().unwrap(), dir);
/// ```
pub fn as_dir(&self) -> Result<Rc<PublicDirectory>> {
Ok(match self {
Self::Dir(dir) => Rc::clone(dir),
_ => bail!(FsError::NotADirectory),
})
}
/// Casts a node to a mutable directory.
pub(crate) fn as_dir_mut(&mut self) -> Result<&mut Rc<PublicDirectory>> {
Ok(match self {
Self::Dir(dir) => dir,
_ => bail!(FsError::NotADirectory),
})
}
/// Casts a node to a file.
///
/// # Examples
///
/// ```
/// use std::rc::Rc;
/// use wnfs::public::{PublicFile, PublicNode};
/// use chrono::Utc;
/// use libipld_core::cid::Cid;
///
/// let file = PublicFile::new_rc(Utc::now(), Cid::default());
/// let node = PublicNode::File(Rc::clone(&file));
///
/// assert_eq!(node.as_file().unwrap(), file);
/// ```
pub fn as_file(&self) -> Result<Rc<PublicFile>> {
Ok(match self {
Self::File(file) => Rc::clone(file),
_ => bail!(FsError::NotAFile),
})
}
/// Tries to resolve this node as a file. Fails with `NotAFile` otherwise.
pub fn as_file_mut(&mut self) -> Result<&mut Rc<PublicFile>> {
match self {
Self::File(file) => Ok(file),
_ => bail!(FsError::NotAFile),
}
}
/// Returns true if underlying node is a directory.
///
/// # Examples
///
/// ```
/// use wnfs::public::{PublicDirectory, PublicNode};
/// use chrono::Utc;
///
/// let dir = PublicDirectory::new_rc(Utc::now());
/// let node = PublicNode::Dir(dir);
///
/// assert!(node.is_dir());
/// ```
pub fn is_dir(&self) -> bool {
matches!(self, Self::Dir(_))
}
/// Returns true if the underlying node is a file.
///
/// # Examples
///
/// ```
/// use wnfs::public::{PublicFile, PublicNode};
/// use chrono::Utc;
/// use libipld_core::cid::Cid;
///
/// let file = PublicFile::new_rc(Utc::now(), Cid::default());
/// let node = PublicNode::File(file);
///
/// assert!(node.is_file());
/// ```
pub fn is_file(&self) -> bool {
matches!(self, Self::File(_))
}
/// Serializes a node to the block store and returns its CID.
pub async fn store(&self, store: &impl BlockStore) -> Result<Cid> {
Ok(match self {
Self::File(file) => file.store(store).await?,
Self::Dir(dir) => dir.store(store).await?,
})
}
/// Loads a node from the block store.
#[inline]
pub async fn load(cid: &Cid, store: &impl BlockStore) -> Result<Self> {
store.get_deserializable(cid).await
}
}
impl Id for PublicNode {
fn get_id(&self) -> String {
match self {
PublicNode::File(file) => file.get_id(),
PublicNode::Dir(dir) => dir.get_id(),
}
}
}
impl PartialEq for PublicNode {
fn eq(&self, other: &PublicNode) -> bool {
match (self, other) {
(Self::File(self_file), Self::File(other_file)) => {
Rc::ptr_eq(self_file, other_file) || self_file == other_file
}
(Self::Dir(self_dir), Self::Dir(other_dir)) => {
Rc::ptr_eq(self_dir, other_dir) || self_dir == other_dir
}
_ => false,
}
}
}
impl From<PublicFile> for PublicNode {
fn from(file: PublicFile) -> Self {
Self::File(Rc::new(file))
}
}
impl From<PublicDirectory> for PublicNode {
fn from(dir: PublicDirectory) -> Self {
Self::Dir(Rc::new(dir))
}
}
impl<'de> Deserialize<'de> for PublicNode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(match PublicNodeSerializable::deserialize(deserializer)? {
PublicNodeSerializable::File(file) => {
let file = PublicFile::from_serializable(file).map_err(DeError::custom)?;
Self::File(Rc::new(file))
}
PublicNodeSerializable::Dir(dir) => {
let dir = PublicDirectory::from_serializable(dir).map_err(DeError::custom)?;
Self::Dir(Rc::new(dir))
}
})
}
}
/// Implements async deserialization for serde serializable types.
#[async_trait(?Send)]
impl AsyncSerialize for PublicNode {
async fn async_serialize<S, B>(&self, serializer: S, store: &B) -> Result<S::Ok, S::Error>
where
S: Serializer,
B: BlockStore + ?Sized,
{
match self {
Self::File(file) => file.serialize(serializer),
Self::Dir(dir) => dir.async_serialize(serializer, store).await,
}
}
}
impl RemembersCid for PublicNode {
fn persisted_as(&self) -> &OnceCell<Cid> {
match self {
PublicNode::File(file) => (*file).persisted_as(),
PublicNode::Dir(dir) => (*dir).persisted_as(),
}
}
}
//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use crate::public::{PublicDirectory, PublicFile, PublicNode};
use chrono::Utc;
use libipld_core::cid::Cid;
use wnfs_common::MemoryBlockStore;
#[async_std::test]
async fn serialized_public_node_can_be_deserialized() {
let store = &MemoryBlockStore::default();
let dir_node: PublicNode = PublicDirectory::new(Utc::now()).into();
let file_node: PublicNode = PublicFile::new(Utc::now(), Cid::default()).into();
let dir_cid = dir_node.store(store).await.unwrap();
let file_cid = file_node.store(store).await.unwrap();
let loaded_file_node = PublicNode::load(&file_cid, store).await.unwrap();
let loaded_dir_node = PublicNode::load(&dir_cid, store).await.unwrap();
assert_eq!(loaded_file_node, file_node);
assert_eq!(loaded_dir_node, dir_node);
}
}
#[cfg(test)]
mod snapshot_tests {
use super::*;
use chrono::TimeZone;
use wnfs_common::utils::SnapshotBlockStore;
#[async_std::test]
async fn public_file_and_directory_nodes() {
let store = &SnapshotBlockStore::default();
let time = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap();
let dir_node: PublicNode = PublicDirectory::new(time).into();
let file_node: PublicNode = PublicFile::new(time, Cid::default()).into();
let dir_cid = dir_node.store(store).await.unwrap();
let file_cid = file_node.store(store).await.unwrap();
let dir = store.get_block_snapshot(&dir_cid).await.unwrap();
let file = store.get_block_snapshot(&file_cid).await.unwrap();
insta::assert_json_snapshot!(dir);
insta::assert_json_snapshot!(file);
}
#[async_std::test]
async fn public_fs() {
let store = &SnapshotBlockStore::default();
let time = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap();
let paths = [
vec!["text.txt".into()],
vec!["music".into(), "jazz".into()],
vec!["videos".into(), "movies".into(), "anime".into()],
];
let root_dir = &mut PublicDirectory::new_rc(time);
let _ = root_dir.store(store).await.unwrap();
for path in paths.iter() {
root_dir
.write(path, Cid::default(), time, store)
.await
.unwrap();
}
let _ = root_dir.store(store).await.unwrap();
let values = store.get_all_block_snapshots().unwrap();
insta::assert_json_snapshot!(values)
}
}