summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-08-13 11:48:38 +0100
committerdiogo464 <[email protected]>2025-08-13 11:48:38 +0100
commitfa28b81ffb4b695c057d38d86c9ccb526d2539c2 (patch)
treea108a88a0d9f94e86eeed8b727f556c852b7ab93
parentf4840684e5ef272f488cdb9757477a65ac9bb830 (diff)
cli: added directory sizes
-rw-r--r--src/main.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index ebbc817..68b82ab 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -531,6 +531,26 @@ impl Default for Fs {
531 } 531 }
532} 532}
533 533
534pub fn compute_directory_sizes(fs: &mut Fs) {
535 fn compute(fs: &mut Fs, node_id: FsNodeId) {
536 let node = &fs.nodes[node_id];
537 if node.kind == FsNodeKind::File {
538 return;
539 }
540
541 let children = node.children.values().copied().collect::<Vec<_>>();
542 children.into_iter().for_each(|id| compute(fs, id));
543
544 let mut size = 0;
545 for child in fs.nodes[node_id].children.values() {
546 let child = &fs.nodes[*child];
547 size += child.size;
548 }
549 fs.nodes[node_id].size = size;
550 }
551 compute(fs, fs.root);
552}
553
534pub fn apply(fs: &mut Fs, op: &Operation) -> Result<(), FsError> { 554pub fn apply(fs: &mut Fs, op: &Operation) -> Result<(), FsError> {
535 match &op.data { 555 match &op.data {
536 OperationData::CreateFile(data) => apply_create_file(fs, &op.header, &data), 556 OperationData::CreateFile(data) => apply_create_file(fs, &op.header, &data),
@@ -951,6 +971,8 @@ fn cmd_ls(args: LsArgs) {
951 let mut fs = Fs::default(); 971 let mut fs = Fs::default();
952 let ops = common_read_log_file(&args.common); 972 let ops = common_read_log_file(&args.common);
953 ops.iter().for_each(|op| apply(&mut fs, op).unwrap()); 973 ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
974 compute_directory_sizes(&mut fs);
975
954 let node_id = match &args.path { 976 let node_id = match &args.path {
955 Some(path) => find_node(&fs, path), 977 Some(path) => find_node(&fs, path),
956 None => Some(fs.root), 978 None => Some(fs.root),
@@ -999,8 +1021,8 @@ fn write_node(
999 if !recurse && (filter_type == None || filter_type == Some(FsNodeKind::Directory)) { 1021 if !recurse && (filter_type == None || filter_type == Some(FsNodeKind::Directory)) {
1000 writeln!( 1022 writeln!(
1001 writer, 1023 writer,
1002 "{}\tdir\t{}\t-\t-\t{}", 1024 "{}\tdir\t{}\t-\t{}\t{}",
1003 path, node.lastmod, node.author 1025 path, node.lastmod, node.size, node.author
1004 ) 1026 )
1005 .unwrap(); 1027 .unwrap();
1006 } 1028 }