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

Fix for / at the end for add command #166

Merged
merged 6 commits into from
Mar 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ repository = "https://github.com/datablockset/blockset"
blockset-lib = { path = "blockset-lib", version = "0.4.2" }
io-trait = "0.10.0"
io-impl = "0.10.0"
io-test = "0.10.0"
io-test = "0.10.1"
wasm-bindgen-test = "0.3.42"
nanvm-lib = "0.0.3"
38 changes: 31 additions & 7 deletions blockset-lib/src/app/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ fn dir_to_json<M: Manager>(
to_json(m.new_js_object(list)).map_err(|_| invalid_input("to_json"))
}

fn calculate_len(files: &[(String, u64)], state: &mut State) -> usize {
fn calculate_len(files: &[(String, u64)], state: &mut State) {
// JSON size:
// `{` +
// `"` + path + `":"` + 45 + `",` = path.len() + 51
let (total, json_len) = files.iter().fold((0, 1), |(total, json_len), (path, len)| {
(total + len, json_len + path.len() + 51)
state.total = files.iter().fold(1, |total, (path, len)| {
total + len + (path.len() as u64) + 51
});
state.total += total + json_len as u64;
json_len
}

fn normalize_path(path: &str) -> &str {
if let Some(s_path) = path.strip_suffix('/') {
s_path
} else {
path
}
}

impl<'a, T: Io, S: 'a + TreeAdd, F: Fn(&'a T) -> S> Add<'a, T, S, F> {
Expand Down Expand Up @@ -114,13 +120,31 @@ impl<'a, T: Io, S: 'a + TreeAdd, F: Fn(&'a T) -> S> Add<'a, T, S, F> {
fn path_to_json(&mut self, path: &str) -> io::Result<String> {
self.calculate_and_add_files(path, read_dir_recursive(self.io, path)?)
}
pub fn add_dir(&mut self, path: &str) -> io::Result<String> {
// TODO: move it to unit tests.
fn check(&mut self, cursor: &Cursor<String>) {
assert!(self.p.current + cursor.position() == self.p.total);
}
fn mem_to_tree(&mut self, cursor: &mut Cursor<String>) -> io::Result<String> {
read_to_tree(
(self.storage)(self.io),
Cursor::new(self.path_to_json(path)?),
cursor,
&mut self.status,
self.display_new,
self.p,
)
}
pub fn add_dir(&mut self, path: &str) -> io::Result<String> {
let mut cursor = Cursor::new(self.path_to_json(path)?);
let result = self.mem_to_tree(&mut cursor)?;
self.check(&cursor);
Ok(result)
}
pub fn add_file_or_dir(&mut self, path: &str, metadata: T::Metadata) -> io::Result<String> {
if metadata.is_dir() {
self.add_dir(normalize_path(path))
} else {
self.p.total = metadata.len();
self.add_file(path)
}
}
}
14 changes: 3 additions & 11 deletions blockset-lib/src/app/add_entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io;

use io_trait::{Io, Metadata};
use io_trait::Io;

use crate::{
cdt::tree_add::TreeAdd,
Expand Down Expand Up @@ -30,12 +30,7 @@ fn add_file_or_dir<'a, T: Io, S: 'a + TreeAdd>(
current: 0,
},
};
if io.metadata(&path)?.is_dir() {
add.add_dir(&path)
} else {
add.p.total = io.metadata(&path)?.len();
add.add_file(&path)
}
add.add_file_or_dir(&path, add.io.metadata(&path)?)
}

pub fn add_entry<'a, T: Io, S: 'a + TreeAdd>(
Expand All @@ -44,10 +39,7 @@ pub fn add_entry<'a, T: Io, S: 'a + TreeAdd>(
storage: &'a impl Fn(&'a T) -> S,
display_new: bool,
) -> io::Result<()> {
let mut path = posix_path(&a.next().ok_or(invalid_input("missing file name"))?);
if path.ends_with('/') {
path.pop();
}
let path = posix_path(&a.next().ok_or(invalid_input("missing file name"))?);
let to_posix_eol = is_to_posix_eol(a)?;
let k = add_file_or_dir(io, storage, to_posix_eol, display_new, path)?;
io.stdout().println([k.as_str()])
Expand Down
11 changes: 3 additions & 8 deletions blockset-lib/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::{
uint::u224::U224,
};

use self::add::posix_path;

fn set_progress(
state: &mut StatusLine<'_, impl Io>,
display_new: bool,
Expand Down Expand Up @@ -140,7 +142,7 @@ pub fn run(io: &impl Io) -> io::Result<()> {
"add" => add_entry(io, &mut a, &|io| ForestTreeAdd::new(FileForest(io)), true),
"get" => {
let d = get_hash(&mut a)?;
let path = a.next().ok_or(invalid_input("missing file name"))?;
let path = posix_path(a.next().ok_or(invalid_input("missing file name"))?.as_str());
let w = &mut io.create(&path)?;
FileForest(io).restore(&ForestNodeId::new(NodeType::Root, &d), w, io)
}
Expand Down Expand Up @@ -393,14 +395,7 @@ mod test {
let mut a = io.args();
a.next().unwrap();
run(&mut io).unwrap();
// add_dir(&io, "a").unwrap();
io.stdout.to_stdout()
/*
assert_eq!(
result,
"add-dir: {\"b.txt\":12,\"d.txt\":0,\"e/f.txt\":0}\n"
);
*/
};
let a = f("a");
let b = f("a/");
Expand Down
Loading