From 895b6ff4cb5bc069781a726f7355acee7749ac19 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 8 Aug 2025 19:34:39 +0100 Subject: added blob size --- fctdrive/src/main.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/fctdrive/src/main.rs b/fctdrive/src/main.rs index 78b3a28..12955bd 100644 --- a/fctdrive/src/main.rs +++ b/fctdrive/src/main.rs @@ -3,6 +3,7 @@ use std::{ fmt::Display, fs::File, io::{BufWriter, Read as _, Write}, + os::unix::fs::MetadataExt, path::{Path, PathBuf}, str::FromStr, sync::{Arc, Mutex}, @@ -122,6 +123,11 @@ pub fn blob_hash_file(file_path: &Path) -> std::io::Result { Ok(BlobId(hasher.finalize().try_into().unwrap())) } +pub fn blob_size(store: &BlobStore, blob_id: &BlobId) -> std::io::Result { + let blob_path = blob_path(store, blob_id); + Ok(blob_path.metadata()?.size()) +} + const OPERATION_KIND_CREATE_FILE: &'static str = "create_file"; const OPERATION_KIND_CREATE_DIR: &'static str = "create_dir"; const OPERATION_KIND_REMOVE: &'static str = "remove"; @@ -155,11 +161,13 @@ impl FromStr for Operation { OPERATION_KIND_CREATE_FILE => { let path_str = iter.next().ok_or_else(err)?; let blob_str = iter.next().ok_or_else(err)?; + let size_str = iter.next().ok_or_else(err)?; let path = path_str.parse().map_err(|_| err())?; let blob = blob_str.parse().map_err(|_| err())?; + let size = size_str.parse().map_err(|_| err())?; - OperationData::CreateFile(OperationCreateFile { path, blob }) + OperationData::CreateFile(OperationCreateFile { path, blob, size }) } OPERATION_KIND_CREATE_DIR => { let path_str = iter.next().ok_or_else(err)?; @@ -208,8 +216,8 @@ impl Display for Operation { match &self.data { OperationData::CreateFile(data) => write!( f, - "{}\t{}\t{}", - OPERATION_KIND_CREATE_FILE, data.path, data.blob + "{}\t{}\t{}\t{}", + OPERATION_KIND_CREATE_FILE, data.path, data.blob, data.size ), OperationData::CreateDir(data) => { write!(f, "{}\t{}", OPERATION_KIND_CREATE_DIR, data.path) @@ -241,6 +249,7 @@ pub enum OperationData { pub struct OperationCreateFile { pub path: DrivePath, pub blob: BlobId, + pub size: u64, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -439,6 +448,7 @@ pub struct FsNode { pub lastmod: u64, pub children: HashMap, pub blob: BlobId, + pub size: u64, } slotmap::new_key_type! { pub struct FsNodeId; } @@ -457,6 +467,7 @@ impl Default for Fs { lastmod: 0, children: Default::default(), blob: Default::default(), + size: 0, }); Self { root, nodes } } @@ -495,6 +506,7 @@ pub fn apply_create_file( lastmod: header.timestamp, children: Default::default(), blob: Default::default(), + size: 0, }); fs.nodes[parent_id].children.insert(comp.clone(), id); id @@ -528,6 +540,7 @@ pub fn apply_create_file( lastmod: header.timestamp, children: Default::default(), blob: data.blob.clone(), + size: data.size, }); fs.nodes[parent_id].children.insert(name, id); } @@ -563,6 +576,7 @@ pub fn apply_create_dir( lastmod: header.timestamp, children: Default::default(), blob: Default::default(), + size: 0, }); fs.nodes[parent_id].children.insert(comp.clone(), id); id @@ -587,6 +601,7 @@ pub fn apply_create_dir( lastmod: header.timestamp, children: Default::default(), blob: Default::default(), + size: 0, }); let parent = &mut fs.nodes[parent_id]; @@ -780,12 +795,13 @@ fn main() { } fn cmd_create_file(args: CreateFileArgs) { + let store = common_create_blob_store(&args.common); let file_blob_id = blob_hash_file(&args.file).unwrap(); + let file_blob_size = blob_size(&store, &file_blob_id).unwrap(); let _lock = common_write_lock(&args.common); let mut fs = Fs::default(); let mut ops = common_read_log_file(&args.common); - let store = common_create_blob_store(&args.common); ops.iter().for_each(|op| apply(&mut fs, op).unwrap()); let timestamp = args.timestamp.unwrap_or_else(get_timestamp); @@ -801,6 +817,7 @@ fn cmd_create_file(args: CreateFileArgs) { data: OperationData::CreateFile(OperationCreateFile { path: args.path, blob: file_blob_id, + size: file_blob_size, }), }; apply(&mut fs, &new_op).unwrap(); @@ -917,15 +934,15 @@ fn write_node( FsNodeKind::File => { writeln!( writer, - "{}\tfile\t{}\t{}\t{}", - path, node.lastmod, node.blob, node.author + "{}\tfile\t{}\t{}\t{}\t{}", + path, node.lastmod, node.blob, node.size, node.author ) .unwrap(); } FsNodeKind::Directory => { writeln!( writer, - "{}\tdir\t{}\t-\t{}", + "{}\tdir\t{}\t-\t-\t{}", path, node.lastmod, node.author ) .unwrap(); @@ -988,6 +1005,7 @@ fn cmd_import(args: ImportArgs) { let drive_path = rel.to_str().unwrap().parse::().unwrap(); let blob_id = blob_hash_file(&file).unwrap(); blob_import_file(&store, ImportMode::HardLink, &blob_id, &file).unwrap(); + let blob_size = blob_size(&store, &blob_id).unwrap(); let op = Operation { header: OperationHeader { timestamp, @@ -997,6 +1015,7 @@ fn cmd_import(args: ImportArgs) { data: OperationData::CreateFile(OperationCreateFile { path: drive_path, blob: blob_id, + size: blob_size, }), }; ops.push(op); -- cgit