From ae91f77d02bfac0e9137b32a05ffe72e99436623 Mon Sep 17 00:00:00 2001 From: Florian Stecker Date: Tue, 13 Feb 2024 17:06:18 -0500 Subject: [PATCH] Use Arc instead of Rc to prepare for shared tree cache --- src/addrmap.rs | 3 ++- src/btrfs_lookup.rs | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/addrmap.rs b/src/addrmap.rs index 5f9b827..9643ea8 100644 --- a/src/addrmap.rs +++ b/src/addrmap.rs @@ -1,4 +1,5 @@ use std::rc::Rc; +use std::sync::Arc; use crate::btrfs_structs::{ParseBin, Key, ChunkItem, Value, Superblock, ParseError, NODE_SIZE}; use crate::btrfs_lookup::Tree; @@ -14,7 +15,7 @@ impl AddressMap { let chunk_tree = Tree { image: image, - addr_map: Rc::new(bootstrap_addr), + addr_map: Arc::new(bootstrap_addr), root_addr_log: superblock.chunk_root, }; diff --git a/src/btrfs_lookup.rs b/src/btrfs_lookup.rs index bc1df67..843a17b 100644 --- a/src/btrfs_lookup.rs +++ b/src/btrfs_lookup.rs @@ -1,5 +1,6 @@ use std::convert::identity; use std::rc::Rc; +use std::sync::Arc; use std::ops::{Deref, RangeBounds, Bound}; use crate::btrfs_structs::{Leaf, Key, Item, InteriorNode, Node, ParseError, ParseBin, Value, Superblock, ItemType, ZERO_KEY}; @@ -9,18 +10,18 @@ use crate::addrmap::{node_at_log, AddressMap}; /// and handles the tree traversal and the virtual address translation. pub struct Tree<'a> { pub image: &'a [u8], - pub addr_map: Rc, + pub addr_map: Arc, pub root_addr_log: u64, } impl<'a> Tree<'a> { pub fn new>(image: &'a [u8], tree_id: T) -> Result, ParseError> { - let addr_map = Rc::new(AddressMap::new(image)?); + let addr_map = Arc::new(AddressMap::new(image)?); let superblock = Superblock::parse(&image[0x10000..])?; let root_tree = Tree { image, - addr_map: Rc::clone(&addr_map), + addr_map: Arc::clone(&addr_map), root_addr_log: superblock.root }; let tree_root_item = root_tree.find_key(Key::new(tree_id.into(), ItemType::Root, 0))?; @@ -34,26 +35,26 @@ impl<'a> Tree<'a> { } pub fn root(image: &'a [u8]) -> Result, ParseError> { - let addr_map = Rc::new(AddressMap::new(image)?); + let addr_map = Arc::new(AddressMap::new(image)?); let superblock = Superblock::parse(&image[0x10000..])?; Ok(Tree { image, addr_map, root_addr_log: superblock.root }) } pub fn chunk(image: &'a [u8]) -> Result, ParseError> { - let addr_map = Rc::new(AddressMap::new(image)?); + let addr_map = Arc::new(AddressMap::new(image)?); let superblock = Superblock::parse(&image[0x10000..])?; Ok(Tree { image, addr_map, root_addr_log: superblock.chunk_root }) } /// Read a node given its logical address - pub fn get_node(&self, addr: u64) -> Result, ParseError> { + pub fn get_node(&self, addr: u64) -> Result, ParseError> { // eprintln!("Reading node at {:x}", addr); let node_data = node_at_log(self.image, self.addr_map.as_ref(), addr)?; let node = Node::parse(node_data)?; - Ok(Rc::new(node)) + Ok(Arc::new(node)) } }