Skip to content

Commit

Permalink
Revert "feat(autonomi): keep filesize in metadata"
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvandijke authored Nov 9, 2024
1 parent 4b6adf6 commit 035fbdb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 64 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,9 @@ jobs:
run: cargo clippy --all-targets --all-features -- -Dwarnings

- name: Check documentation
# Deny certain `rustdoc` lints that are unwanted with `RUSTDOCFLAGS`. See
# https://doc.rust-lang.org/rustdoc/lints.html for lints that are 'warning' by default.
#
# We exclude autonomi-cli because it is not published and conflicts with the `autonomi` crate name,
# resulting in an error when building docs.
run: RUSTDOCFLAGS="--deny=warnings" cargo doc --no-deps --workspace --exclude=autonomi-cli
# Deny certain `rustdoc` lints that are unwanted.
# See https://doc.rust-lang.org/rustdoc/lints.html for lints that are 'warning' by default.
run: RUSTDOCFLAGS="--deny=warnings" cargo doc --no-deps

- name: Check local is not a default feature
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion autonomi/examples/metamask/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function externalSignerPrivateDataPutToVault(peerAddr) {
const privateArchive = new autonomi.PrivateArchive();

// Add our data's data map chunk to the private archive
privateArchive.addFile("test", privateDataAccess, autonomi.createMetadata(data.length));
privateArchive.addNewFile("test", privateDataAccess);

// Get the private archive's bytes
const privateArchiveBytes = privateArchive.bytes();
Expand Down
20 changes: 14 additions & 6 deletions autonomi/src/client/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,29 @@ pub struct Metadata {
pub created: u64,
/// Last file modification time taken from local file system. See [`std::fs::Metadata::modified`] for details per OS.
pub modified: u64,
/// File size in bytes
pub size: u64,
}

impl Metadata {
/// Create a new metadata struct with the current time as uploaded, created and modified.
pub fn new_with_size(size: u64) -> Self {
/// Create a new metadata struct
pub fn new() -> Self {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or(Duration::from_secs(0))
.as_secs();

Self {
uploaded: now,
created: now,
modified: now,
size,
}
}
}

impl Default for Metadata {
fn default() -> Self {
Self::new()
}
}

impl Archive {
/// Create a new emtpy local archive
/// Note that this does not upload the archive to the network
Expand Down Expand Up @@ -102,6 +104,12 @@ impl Archive {
self.map.insert(path, (data_addr, meta));
}

/// Add a file to a local archive, with default metadata
/// Note that this does not upload the archive to the network
pub fn add_new_file(&mut self, path: PathBuf, data_addr: DataAddr) {
self.map.insert(path, (data_addr, Metadata::new()));
}

/// List all files in the archive
pub fn files(&self) -> Vec<(PathBuf, Metadata)> {
self.map
Expand Down
6 changes: 6 additions & 0 deletions autonomi/src/client/archive_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ impl PrivateArchive {
self.map.insert(path, (data_map, meta));
}

/// Add a file to a local archive, with default metadata
/// Note that this does not upload the archive to the network
pub fn add_new_file(&mut self, path: PathBuf, data_map: PrivateDataAccess) {
self.map.insert(path, (data_map, Metadata::new()));
}

/// List all files in the archive
pub fn files(&self) -> Vec<(PathBuf, Metadata)> {
self.map
Expand Down
5 changes: 1 addition & 4 deletions autonomi/src/client/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ impl Client {
tracing::debug!("Encryption took: {:.2?}", now.elapsed());
let map_xor_name = *data_map_chunk.address().xorname();

let metadata = metadata_from_entry(&entry);
archive.add_file(path, map_xor_name, metadata);
archive.add_file(path, map_xor_name, Metadata::new());
}

let root_serialized = rmp_serde::to_vec(&archive)?;
Expand All @@ -235,7 +234,6 @@ pub(crate) fn metadata_from_entry(entry: &walkdir::DirEntry) -> Metadata {
uploaded: 0,
created: 0,
modified: 0,
size: 0,
};
}
};
Expand Down Expand Up @@ -268,6 +266,5 @@ pub(crate) fn metadata_from_entry(entry: &walkdir::DirEntry) -> Metadata {
.as_secs(),
created,
modified,
size: fs_metadata.len(),
}
}
47 changes: 8 additions & 39 deletions autonomi/src/client/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use wasm_bindgen::prelude::*;
/// const dataAddr = await client.putData(new Uint8Array([0, 1, 2, 3]), wallet);
///
/// const archive = new Archive();
/// archive.addFile("foo", dataAddr, createMetadata(4));
/// archive.addNewFile("foo", dataAddr);
///
/// const archiveAddr = await client.putArchive(archive, wallet);
/// const archiveFetched = await client.getArchive(archiveAddr);
Expand Down Expand Up @@ -170,32 +170,14 @@ impl JsClient {

mod archive {
use super::*;
use crate::client::{
address::str_to_addr,
archive::{Archive, Metadata},
};
use crate::client::{address::str_to_addr, archive::Archive};
use std::path::PathBuf;
use wasm_bindgen::JsError;

/// Structure mapping paths to data addresses.
#[wasm_bindgen(js_name = Archive)]
pub struct JsArchive(Archive);

/// Create new metadata with the current time as uploaded, created and modified.
///
/// # Example
///
/// ```js
/// const metadata = createMetadata(BigInt(3));
/// const archive = new atnm.Archive();
/// archive.addFile("foo", addr, metadata);
/// ```
#[wasm_bindgen(js_name = createMetadata)]
pub fn create_metadata(size: u64) -> Result<JsValue, JsError> {
let metadata = Metadata::new_with_size(size);
Ok(serde_wasm_bindgen::to_value(&metadata)?)
}

#[wasm_bindgen(js_class = Archive)]
impl JsArchive {
/// Create a new archive.
Expand All @@ -205,17 +187,11 @@ mod archive {
}

/// Add a new file to the archive.
#[wasm_bindgen(js_name = addFile)]
pub fn add_file(
&mut self,
path: String,
data_addr: String,
metadata: JsValue,
) -> Result<(), JsError> {
#[wasm_bindgen(js_name = addNewFile)]
pub fn add_new_file(&mut self, path: String, data_addr: String) -> Result<(), JsError> {
let path = PathBuf::from(path);
let data_addr = str_to_addr(&data_addr)?;
let metadata: Metadata = serde_wasm_bindgen::from_value(metadata)?;
self.0.add_file(path, data_addr, metadata);
self.0.add_new_file(path, data_addr);

Ok(())
}
Expand Down Expand Up @@ -273,7 +249,6 @@ mod archive {

mod archive_private {
use super::*;
use crate::client::archive::Metadata;
use crate::client::archive_private::{PrivateArchive, PrivateArchiveAccess};
use crate::client::data_private::PrivateDataAccess;
use crate::client::payment::Receipt;
Expand All @@ -293,17 +268,11 @@ mod archive_private {
}

/// Add a new file to the private archive.
#[wasm_bindgen(js_name = addFile)]
pub fn add_file(
&mut self,
path: String,
data_map: JsValue,
metadata: JsValue,
) -> Result<(), JsError> {
#[wasm_bindgen(js_name = addNewFile)]
pub fn add_new_file(&mut self, path: String, data_map: JsValue) -> Result<(), JsError> {
let path = PathBuf::from(path);
let data_map: PrivateDataAccess = serde_wasm_bindgen::from_value(data_map)?;
let metadata: Metadata = serde_wasm_bindgen::from_value(metadata)?;
self.0.add_file(path, data_map, metadata);
self.0.add_new_file(path, data_map);

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions autonomi/tests-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ describe('autonomi', function () {
const data = randomData(32);
const addr = await client.putData(data, wallet);
const archive = new atnm.Archive();
archive.addFile("foo", addr, atnm.createMetadata(BigInt(data.length)));
archive.addNewFile("foo", addr);
const archiveAddr = await client.putArchive(archive, wallet);

const archiveFetched = await client.getArchive(archiveAddr);

assert.deepEqual(archive.map(), archiveFetched.map());
assert.deepEqual(archive, archiveFetched);
});

it('writes archive to vault and fetches it', async () => {
Expand All @@ -59,7 +59,7 @@ describe('autonomi', function () {
const secretKey = atnm.genSecretKey();

const archive = new atnm.Archive();
archive.addFile('foo', addr, atnm.createMetadata(BigInt(data.length)));
archive.addNewFile('foo', addr);
const archiveAddr = await client.putArchive(archive, wallet);

const userData = new atnm.UserData();
Expand Down
6 changes: 1 addition & 5 deletions autonomi/tests/external_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ async fn external_signer_put() -> eyre::Result<()> {
.await?;

let mut private_archive = PrivateArchive::new();
private_archive.add_file(
"test-file".into(),
private_data_access,
Metadata::new_with_size(data.len() as u64),
);
private_archive.add_file("test-file".into(), private_data_access, Metadata::default());

let archive_serialized = private_archive.into_bytes()?;

Expand Down
1 change: 1 addition & 0 deletions sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ use tokio::sync::{
oneshot,
};
use tokio::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use {
sn_protocol::storage::{
try_deserialize_record, try_serialize_record, RecordHeader, RecordKind,
Expand Down

0 comments on commit 035fbdb

Please sign in to comment.