11use crate :: util:: { config, GPath } ;
2+ use bincode:: { Decode , Encode } ;
23use rfuse3:: raw:: reply:: ReplyEntry ;
34use rfuse3:: FileType ;
45use serde:: { Deserialize , Serialize } ;
@@ -14,7 +15,7 @@ pub struct TreeStorage {
1415 db : Db ,
1516}
1617
17- #[ derive( Serialize , Deserialize , Clone ) ]
18+ #[ derive( Serialize , Deserialize , Clone , Encode , Decode ) ]
1819pub struct StorageItem {
1920 inode : u64 ,
2021 parent : u64 ,
@@ -80,12 +81,12 @@ impl TreeStorage {
8081 children : Vec :: new ( ) ,
8182 hash : item. hash ,
8283 } ;
83-
84+ let config = bincode :: config :: standard ( ) ;
8485 // Insert an item into db and update the parent item's children list.
8586 self . db
8687 . insert (
8788 inode. to_be_bytes ( ) ,
88- bincode:: serialize ( & storage_item) . map_err ( Error :: other) ?,
89+ bincode:: encode_to_vec ( & storage_item, config ) . map_err ( Error :: other) ?,
8990 )
9091 . map_err ( Error :: other) ?;
9192
@@ -97,7 +98,7 @@ impl TreeStorage {
9798 self . db
9899 . insert (
99100 parent. to_be_bytes ( ) ,
100- bincode:: serialize ( & parent_item) . map_err ( Error :: other) ?,
101+ bincode:: encode_to_vec ( & parent_item, config ) . map_err ( Error :: other) ?,
101102 )
102103 . map_err ( Error :: other) ?;
103104 }
@@ -122,10 +123,12 @@ impl TreeStorage {
122123 if storage_item. parent != 0 {
123124 let mut parent_item: StorageItem = self . get_storage_item ( storage_item. parent ) ?;
124125 parent_item. children . retain ( |& x| x != inode) ;
126+ let config = bincode:: config:: standard ( ) ;
127+
125128 self . db
126129 . insert (
127130 storage_item. parent . to_be_bytes ( ) ,
128- bincode:: serialize ( & parent_item) . map_err ( Error :: other) ?,
131+ bincode:: encode_to_vec ( & parent_item, config ) . map_err ( Error :: other) ?,
129132 )
130133 . map_err ( Error :: other) ?;
131134 }
@@ -140,10 +143,11 @@ impl TreeStorage {
140143 pub fn append_child ( & self , parent : u64 , inode : u64 ) -> io:: Result < ( ) > {
141144 let mut st = self . get_storage_item ( parent) ?;
142145 st. children . push ( inode) ;
146+ let config = bincode:: config:: standard ( ) ;
143147 self . db
144148 . insert (
145149 parent. to_be_bytes ( ) ,
146- bincode:: serialize ( & st) . map_err ( Error :: other) ?,
150+ bincode:: encode_to_vec ( & st, config ) . map_err ( Error :: other) ?,
147151 )
148152 . map_err ( Error :: other) ?;
149153 Ok ( ( ) )
@@ -163,7 +167,8 @@ impl TreeStorage {
163167 pub fn get_storage_item ( & self , inode : u64 ) -> io:: Result < StorageItem > {
164168 match self . db . get ( inode. to_be_bytes ( ) ) ? {
165169 Some ( value) => {
166- let item: StorageItem = bincode:: deserialize ( & value) . map_err ( Error :: other) ?;
170+ let config = bincode:: config:: standard ( ) ;
171+ let ( item , _) = bincode:: decode_from_slice ( & value, config) . map_err ( Error :: other) ?;
167172 Ok ( item)
168173 }
169174 None => Err ( Error :: new ( ErrorKind :: NotFound , "Item not found" ) ) ,
@@ -184,10 +189,11 @@ impl TreeStorage {
184189 pub fn update_item_hash ( & self , inode : u64 , hash : String ) -> io:: Result < ( ) > {
185190 let mut item = self . get_storage_item ( inode) ?;
186191 item. hash = hash;
192+ let config = bincode:: config:: standard ( ) ;
187193 self . db
188194 . insert (
189195 inode. to_be_bytes ( ) ,
190- bincode:: serialize ( & item) . map_err ( Error :: other) ?,
196+ bincode:: encode_to_vec ( & item, config ) . map_err ( Error :: other) ?,
191197 )
192198 . map_err ( Error :: other) ?;
193199 Ok ( ( ) )
0 commit comments