test_helpers.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. // SPDX-License-Identifier: AGPL-3.0-or-later
  2. /// Test data and functions to help with testing.
  3. use btserde::{Error, Result};
  4. use fuse_backend_rs::{
  5. api::filesystem::{ZeroCopyReader, ZeroCopyWriter},
  6. file_buf::FileVolatileSlice,
  7. file_traits::FileReadWriteVolatile,
  8. };
  9. use std::{
  10. cell::RefCell,
  11. fmt::Write as FmtWrite,
  12. fs::File,
  13. io::{Cursor, Write},
  14. };
  15. use vm_memory::bytes::Bytes;
  16. use super::*;
  17. use crate::{crypto::*, sectored_buf::SectoredBuf, Cursor as PioCursor};
  18. /// A smaller default sector size for use in tests.
  19. pub const SECTOR_SZ_DEFAULT: usize = 4096;
  20. pub const PRINCIPAL: [u8; 32] = [
  21. 0x75, 0x28, 0xA9, 0xE0, 0x9D, 0x24, 0xBA, 0xB3, 0x79, 0x56, 0x15, 0x68, 0xFD, 0xA4, 0xE2, 0xA4,
  22. 0xCF, 0xB2, 0xC0, 0xE3, 0x96, 0xAE, 0xA2, 0x6E, 0x45, 0x15, 0x50, 0xED, 0xA6, 0xBE, 0x6D, 0xEC,
  23. ];
  24. pub const PRINCIPAL2: [u8; 32] = [
  25. 0x6E, 0x8A, 0x76, 0x1D, 0xC7, 0x3B, 0x50, 0x90, 0x51, 0x9E, 0x73, 0x9B, 0xEE, 0x7B, 0x02, 0xF9,
  26. 0xB9, 0x17, 0x7C, 0xF6, 0xBB, 0xC8, 0xD5, 0x30, 0xBF, 0x2A, 0xB4, 0xDE, 0x1B, 0x38, 0xCC, 0xF6,
  27. ];
  28. pub const SIGNATURE: [u8; 384] = [
  29. 0x12, 0xB1, 0x09, 0x2F, 0x2B, 0x3C, 0x53, 0xE8, 0x1B, 0x2B, 0x6A, 0xE7, 0x97, 0x42, 0x9D, 0x83,
  30. 0x71, 0x75, 0x65, 0x25, 0xFD, 0xB0, 0x0E, 0x2F, 0xAB, 0x53, 0xB7, 0x03, 0x03, 0x39, 0xEE, 0xE9,
  31. 0x15, 0x19, 0xAB, 0x1A, 0xF1, 0x2C, 0x3C, 0xAB, 0x5C, 0x02, 0xEA, 0xD6, 0xF6, 0x94, 0x36, 0x80,
  32. 0x5F, 0xD1, 0x8E, 0xC4, 0xB9, 0xB5, 0x04, 0x73, 0xBB, 0x77, 0x2C, 0x9C, 0xAF, 0xE6, 0x38, 0xB7,
  33. 0xD8, 0xC1, 0xA3, 0x1B, 0xAD, 0xFB, 0xB2, 0x5E, 0x77, 0xBD, 0x79, 0xDC, 0x4E, 0xEC, 0xEA, 0xFA,
  34. 0x80, 0x9F, 0x12, 0x60, 0xC4, 0xB3, 0x71, 0xAF, 0x1C, 0x28, 0xEE, 0x91, 0x1D, 0x6A, 0x69, 0x31,
  35. 0xA4, 0x5A, 0xAA, 0xBE, 0x0B, 0x20, 0x6A, 0xE1, 0x61, 0x0E, 0x0B, 0x9F, 0x60, 0x73, 0xBE, 0x29,
  36. 0x1F, 0x11, 0x0A, 0x8F, 0x42, 0x8F, 0x80, 0xC1, 0x06, 0x40, 0x29, 0xE9, 0xF1, 0x74, 0x44, 0x69,
  37. 0xCC, 0xD3, 0x05, 0x22, 0x24, 0x87, 0x94, 0x49, 0x4B, 0x5B, 0x62, 0x48, 0x01, 0x7B, 0x05, 0x28,
  38. 0xBE, 0x4D, 0x50, 0x06, 0xE5, 0x67, 0xA8, 0xFB, 0x9A, 0x19, 0x52, 0x57, 0x4B, 0xC4, 0xBB, 0x6A,
  39. 0xF5, 0x0D, 0x67, 0xD0, 0x08, 0x93, 0x81, 0xD2, 0x9E, 0xE2, 0xC8, 0xFA, 0x25, 0xEE, 0x9D, 0xC5,
  40. 0xD8, 0xF4, 0xD5, 0x17, 0xE4, 0xEF, 0xFC, 0x09, 0xB0, 0x09, 0xCA, 0xCA, 0x71, 0x36, 0x61, 0xBE,
  41. 0xC1, 0xD0, 0x53, 0x6D, 0x0C, 0x0A, 0x4D, 0xF1, 0xE0, 0xAB, 0x2C, 0x89, 0x98, 0x81, 0xED, 0x25,
  42. 0xF8, 0x81, 0x8E, 0x2C, 0x46, 0x74, 0x57, 0xCF, 0x5B, 0xE2, 0x14, 0xA5, 0xBF, 0x56, 0xED, 0xD3,
  43. 0x11, 0xE7, 0xB4, 0x8D, 0x89, 0x3A, 0xB2, 0x78, 0xF1, 0xA9, 0x82, 0x9D, 0x3B, 0xE5, 0x6B, 0xD2,
  44. 0xA5, 0xB6, 0xFB, 0x71, 0x0F, 0x98, 0x52, 0x54, 0x1E, 0x98, 0xD2, 0x3B, 0x78, 0x51, 0xD1, 0xE2,
  45. 0x57, 0xAE, 0xB3, 0x34, 0x0E, 0x20, 0x26, 0xCF, 0x4B, 0x1F, 0x12, 0xCA, 0xB1, 0xD5, 0x67, 0x55,
  46. 0xF9, 0xE2, 0xA9, 0x7B, 0xAE, 0x35, 0x35, 0x67, 0xA3, 0xCA, 0xBD, 0xF2, 0x41, 0x4E, 0x3F, 0x7E,
  47. 0x20, 0x72, 0xC2, 0xCC, 0xE4, 0x90, 0x00, 0x04, 0x5D, 0xF7, 0xD9, 0xC7, 0x0C, 0x2C, 0xF8, 0x80,
  48. 0xCB, 0xF2, 0x3E, 0x1A, 0xE9, 0x43, 0x23, 0xAF, 0x62, 0x57, 0xD1, 0x4C, 0xFD, 0x05, 0xB6, 0xB6,
  49. 0x86, 0xB9, 0x86, 0x40, 0xC2, 0x56, 0x45, 0x52, 0xE2, 0x93, 0x62, 0x65, 0x23, 0xE7, 0x1D, 0x89,
  50. 0xCA, 0x40, 0x23, 0x44, 0xDB, 0x56, 0x90, 0x4A, 0x0E, 0xB1, 0xB1, 0xE1, 0x9B, 0x5A, 0x5D, 0x53,
  51. 0xEF, 0x4E, 0xE1, 0x0E, 0x12, 0xA9, 0x7C, 0x11, 0x0B, 0x6A, 0x4C, 0x3A, 0x8F, 0x2B, 0x9F, 0xC6,
  52. 0x42, 0xD7, 0xFE, 0x7D, 0x10, 0x02, 0xBD, 0x31, 0x21, 0xCD, 0xD1, 0x32, 0x04, 0x22, 0xE2, 0x36,
  53. ];
  54. /// The key of the parent of test blocks.
  55. pub static PARENT_KEY: SymKey = {
  56. let key = [
  57. 0x35, 0x3D, 0x8C, 0x95, 0x6C, 0x8D, 0xE6, 0xC0, 0xB0, 0xD5, 0x1C, 0xE9, 0x94, 0xB0, 0x58,
  58. 0xD3, 0x80, 0x46, 0x12, 0x1C, 0xF3, 0x9B, 0x8A, 0xEC, 0x38, 0xD5, 0x8B, 0x05, 0x92, 0x8D,
  59. 0xA1, 0x18,
  60. ];
  61. let iv = [
  62. 0xDA, 0x96, 0x0E, 0xF5, 0x00, 0xDB, 0xC8, 0x4E, 0xFB, 0x83, 0x59, 0x99, 0x6C, 0x18, 0xD1,
  63. 0x27,
  64. ];
  65. SymKey::Aes256Cbc { key, iv }
  66. };
  67. lazy_static! {
  68. static ref INHERIT: Ciphertext<SymKey> = PARENT_KEY
  69. .ser_encrypt(&BLOCK_KEY)
  70. .expect("failed to encrypt block key");
  71. }
  72. /// The key used to encrypt test blocks.
  73. pub static BLOCK_KEY: SymKey = {
  74. let key = [
  75. 0xB2, 0xB3, 0xDA, 0x5A, 0x1A, 0xF6, 0xB3, 0x78, 0x30, 0xAB, 0x1D, 0x33, 0x33, 0xE7, 0xE3,
  76. 0x5B, 0xBB, 0xF9, 0xFE, 0xD0, 0xC1, 0xF7, 0x90, 0x34, 0x69, 0xB7, 0xE7, 0xC6, 0x1C, 0x46,
  77. 0x85, 0x48,
  78. ];
  79. let iv = [
  80. 0xEC, 0x19, 0x59, 0x3A, 0x1D, 0x1E, 0x4A, 0x58, 0x66, 0xC1, 0xD1, 0x9A, 0x61, 0x6E, 0xBA,
  81. 0x16,
  82. ];
  83. SymKey::Aes256Cbc { key, iv }
  84. };
  85. lazy_static! {
  86. pub static ref ROOT_CREDS: ConcreteCreds =
  87. ConcreteCreds::generate().expect("root cred generation failed");
  88. pub static ref NODE_CREDS: ConcreteCreds = {
  89. let mut node_creds = ConcreteCreds::generate().expect("node cred generation failed");
  90. let expires = Epoch::now() + Duration::from_secs(3600);
  91. let writecap = ROOT_CREDS
  92. .issue_writecap(node_creds.principal(), vec![], expires)
  93. .expect("failed to issue writecap to test_helpers::NODE_CREDS");
  94. node_creds.set_writecap(writecap);
  95. node_creds
  96. };
  97. }
  98. pub fn node_creds() -> &'static ConcreteCreds {
  99. &NODE_CREDS
  100. }
  101. pub fn root_creds() -> &'static ConcreteCreds {
  102. &ROOT_CREDS
  103. }
  104. /// Converts the given error to a serde_block_tree error by turning it into a message.
  105. fn convert_err<E: Display>(err: E) -> Error {
  106. Error::Message(err.to_string())
  107. }
  108. pub fn make_principal() -> Principal {
  109. Principal(VarHash::Sha2_256(PRINCIPAL.into()))
  110. }
  111. pub fn make_path_with_root(root: Principal, rel_components: Vec<&str>) -> BlockPath {
  112. let mut components = Vec::with_capacity(rel_components.len());
  113. for component in rel_components {
  114. components.push(component.to_string());
  115. }
  116. BlockPath::new(root, components)
  117. }
  118. pub fn make_path(rel_components: Vec<&str>) -> BlockPath {
  119. make_path_with_root(make_principal(), rel_components)
  120. }
  121. pub fn make_writecap_and_creds(rel_components: Vec<&str>) -> (Writecap, impl Creds) {
  122. let (root_writecap, root_key) = make_self_signed_writecap();
  123. let issued_to = Principal(VarHash::Sha2_256(PRINCIPAL.into()));
  124. (
  125. make_writecap_trusted_by(root_writecap, &root_key, issued_to, rel_components),
  126. root_key,
  127. )
  128. }
  129. pub fn make_writecap(rel_components: Vec<&str>) -> Writecap {
  130. let (writecap, ..) = make_writecap_and_creds(rel_components);
  131. writecap
  132. }
  133. pub fn make_writecap_trusted_by<C: Creds>(
  134. next: Writecap,
  135. trusting_creds: &C,
  136. issued_to: Principal,
  137. path_components: Vec<&str>,
  138. ) -> Writecap {
  139. let hour_hence = Epoch::now() + Duration::from_secs(3600);
  140. let mut writecap = Writecap {
  141. body: WritecapBody {
  142. issued_to,
  143. path: make_path_with_root(next.body.path.root().clone(), path_components),
  144. expires: hour_hence,
  145. signing_key: trusting_creds.public_sign().clone(),
  146. },
  147. signature: Signature::empty(Sign::RSA_PSS_3072_SHA_256),
  148. next: Some(Box::from(next)),
  149. };
  150. trusting_creds
  151. .sign_writecap(&mut writecap)
  152. .map_err(convert_err)
  153. .expect("failed to sign writecap");
  154. writecap
  155. }
  156. pub fn make_key_pair() -> impl Creds {
  157. NODE_CREDS.clone()
  158. }
  159. pub fn make_self_signed_writecap() -> (Writecap, impl Creds) {
  160. let key = make_key_pair();
  161. (make_self_signed_writecap_with(&key), key)
  162. }
  163. pub fn make_self_signed_writecap_with<C: Creds>(key: &C) -> Writecap {
  164. let root_principal = key.principal();
  165. let hour_hence = Epoch::now() + Duration::from_secs(3600);
  166. let mut writecap = Writecap {
  167. body: WritecapBody {
  168. issued_to: root_principal.clone(),
  169. path: make_path_with_root(root_principal, vec![]),
  170. expires: hour_hence,
  171. signing_key: key.public_sign().clone(),
  172. },
  173. signature: Signature::empty(Sign::RSA_PSS_3072_SHA_256),
  174. next: None,
  175. };
  176. key.sign_writecap(&mut writecap)
  177. .map_err(convert_err)
  178. .expect("failed to sign writecap");
  179. writecap
  180. }
  181. pub fn make_block_with() -> SectoredBuf<SecretStream<PioCursor<impl Block>>> {
  182. let block_key = SymKey::generate(SymKeyKind::default()).unwrap();
  183. // Notice that the writecap path contains the block path. If this were not the case, the block
  184. // would be invalid.
  185. let (writecap, creds) = make_writecap_and_creds(vec!["apps"]);
  186. let root_writecap = writecap.next.as_ref().unwrap();
  187. let path = make_path_with_root(root_writecap.body.issued_to.clone(), vec!["apps", "verse"]);
  188. let header = BlockMetaBody::new(&creds).unwrap();
  189. let sig = Signature::copy_from(Sign::RSA_PSS_3072_SHA_256, &SIGNATURE);
  190. let mut stream = BlockStream::new(
  191. SectoredCursor::new(Vec::new(), SECTOR_SZ_DEFAULT).require_sect_sz(false),
  192. creds,
  193. None,
  194. path,
  195. )
  196. .expect("create block stream failed");
  197. stream.meta.body = header;
  198. stream.meta.sig = sig;
  199. let stream = MerkleStream::new(stream).expect("create merkle stream failed");
  200. let stream = SecretStream::new(block_key)
  201. .try_compose(PioCursor::new(stream))
  202. .expect("create secret stream failed");
  203. SectoredBuf::new(stream).expect("failed to compose with SectoredBuf")
  204. }
  205. /// This function can be run as a test to write a new RSA key pair, as two Rust arrays,
  206. /// out to a file.
  207. #[allow(dead_code)]
  208. fn write_test_keys() -> Result<()> {
  209. write_rsa_keys_to_file("test_keys.rs")
  210. }
  211. struct NamedSlice<'a> {
  212. name: &'a str,
  213. slice: &'a [u8],
  214. }
  215. impl<'a> NamedSlice<'a> {
  216. fn new(name: &'a str, slice: &'a [u8]) -> NamedSlice<'a> {
  217. NamedSlice { name, slice }
  218. }
  219. }
  220. fn write_rsa_keys_to_file(path: &str) -> Result<()> {
  221. use openssl::rsa::Rsa;
  222. let rsa = Rsa::generate(3072).map_err(convert_err)?;
  223. let public_der = rsa.public_key_to_der().map_err(convert_err)?;
  224. let private_der = rsa.private_key_to_der().map_err(convert_err)?;
  225. let slices = [
  226. NamedSlice::new("PUBLIC", public_der.as_slice()),
  227. NamedSlice::new("PRIVATE", private_der.as_slice()),
  228. ];
  229. write_to_file(path, slices.into_iter())
  230. }
  231. fn write_to_file<'a, I: Iterator<Item = NamedSlice<'a>>>(path: &str, slices: I) -> Result<()> {
  232. let path = std::path::Path::new(path);
  233. let mut file = File::create(path).map_err(Error::Io)?;
  234. for NamedSlice { name, slice } in slices {
  235. write_slice(&mut file, name, slice)?;
  236. }
  237. Ok(())
  238. }
  239. fn write_slice<W: Write>(output: &mut W, name: &str, slice: &[u8]) -> Result<()> {
  240. const LINE_LEN: usize = 100;
  241. let mut line = String::with_capacity(LINE_LEN);
  242. write!(line, " ").map_err(Error::Format)?;
  243. writeln!(output, "pub const {}: [u8; {}] = [", name, slice.len()).map_err(Error::Io)?;
  244. for byte in slice {
  245. if line.len() + 6 > LINE_LEN {
  246. writeln!(output, "{}", line).map_err(Error::Io)?;
  247. line.clear();
  248. write!(line, " ").map_err(Error::Format)?;
  249. }
  250. write!(line, "0x{:02X?}, ", byte).map_err(Error::Format)?;
  251. }
  252. writeln!(output, "{}", line).map_err(Error::Io)?;
  253. writeln!(output, "];").map_err(Error::Io)?;
  254. writeln!(output).map_err(Error::Io)?;
  255. Ok(())
  256. }
  257. /// A naive randomizer implementation that is intended only for testing.
  258. pub struct Randomizer {
  259. state: [u8; Self::HASH.len()],
  260. buf: [u8; Self::HASH.len()],
  261. }
  262. impl Randomizer {
  263. pub const HASH: HashKind = HashKind::Sha2_256;
  264. pub fn new(seed: [u8; Self::HASH.len()]) -> Randomizer {
  265. Randomizer {
  266. state: seed,
  267. buf: [0u8; Self::HASH.len()],
  268. }
  269. }
  270. }
  271. impl Iterator for Randomizer {
  272. type Item = usize;
  273. fn next(&mut self) -> Option<Self::Item> {
  274. const BYTES: usize = usize::BITS as usize / 8;
  275. Self::HASH
  276. .digest(&mut self.buf, std::iter::once(self.state.as_slice()))
  277. .expect("digest failed");
  278. self.state.copy_from_slice(&self.buf);
  279. let int_bytes = self.buf.as_slice()[..BYTES]
  280. .try_into()
  281. .expect("failed to convert to array");
  282. Some(usize::from_ne_bytes(int_bytes))
  283. }
  284. }
  285. pub fn write_sector(mut buf: &mut [u8], sect_index: usize) {
  286. let data = (sect_index + 1).to_ne_bytes();
  287. for chunk in std::iter::repeat(data).take(buf.len() / data.len()) {
  288. (&mut buf[..chunk.len()]).copy_from_slice(chunk.as_slice());
  289. buf = &mut buf[chunk.len()..];
  290. }
  291. }
  292. pub fn write_fill<W: Write>(mut write: W, sect_sz: usize, sect_ct: usize) {
  293. let mut buf = vec![0u8; sect_sz];
  294. for sect_index in 0..sect_ct {
  295. write_sector(&mut buf, sect_index);
  296. write.write_all(&mut buf).expect("write failed");
  297. }
  298. write.flush().expect("flush failed");
  299. }
  300. pub fn read_check<R: Read>(mut read: R, sect_sz: usize, sect_ct: usize) {
  301. let mut actual = vec![0u8; sect_sz];
  302. let mut expected = vec![0u8; sect_sz];
  303. for sect_index in 0..sect_ct {
  304. write_sector(&mut expected, sect_index);
  305. read.read_exact(&mut actual).expect("read failed");
  306. assert_eq!(expected, actual);
  307. }
  308. }
  309. #[allow(dead_code)]
  310. pub fn write_indices<W: Write + Seek, I: Iterator<Item = usize>>(
  311. mut write: W,
  312. sect_sz: usize,
  313. indices: I,
  314. ) {
  315. let mut buf = vec![0u8; sect_sz];
  316. for sect_index in indices {
  317. let offset = sect_index * sect_sz;
  318. write
  319. .seek(SeekFrom::Start(offset as u64))
  320. .expect("seek failed");
  321. write_sector(&mut buf, sect_index);
  322. write.write_all(&mut buf).expect("write failed");
  323. }
  324. write.flush().expect("flush failed");
  325. }
  326. #[allow(dead_code)]
  327. pub fn read_indices<R: Read + Seek, I: Iterator<Item = usize>>(
  328. mut read: R,
  329. sect_sz: usize,
  330. indices: I,
  331. ) {
  332. let mut actual = vec![0u8; sect_sz];
  333. let mut expected = vec![0u8; sect_sz];
  334. for sect_index in indices {
  335. let offset = sect_index * sect_sz;
  336. read.seek(SeekFrom::Start(offset as u64))
  337. .expect("seek failed");
  338. write_sector(&mut expected, sect_index);
  339. read.read_exact(&mut actual).expect("read failed");
  340. assert_eq!(expected, actual);
  341. }
  342. }
  343. #[allow(dead_code)]
  344. pub fn random_indices<'a>(
  345. rando: &'a mut Randomizer,
  346. sect_ct: usize,
  347. ) -> impl Iterator<Item = usize> + 'a {
  348. rando.take(sect_ct).map(move |e| e % sect_ct)
  349. }
  350. /// Returns an integer array starting at the given value and increasing by one for each subsequent
  351. /// entry.
  352. pub const fn integer_array<const N: usize>(start: u8) -> [u8; N] {
  353. let mut array = [0u8; N];
  354. let mut k = 0usize;
  355. while k < N {
  356. array[k] = start.wrapping_add(k as u8);
  357. k += 1;
  358. }
  359. array
  360. }
  361. /// Trait for types which can be referenced as slices and which support conversion from `Vec<u8>`.
  362. pub trait FromVec: AsRef<[u8]> {
  363. fn from_vec(vec: Vec<u8>) -> Self;
  364. }
  365. impl FromVec for Vec<u8> {
  366. fn from_vec(vec: Vec<u8>) -> Self {
  367. vec
  368. }
  369. }
  370. impl<const N: usize> FromVec for [u8; N] {
  371. fn from_vec(vec: Vec<u8>) -> Self {
  372. assert_eq!(N, vec.len());
  373. let mut buf = [0u8; N];
  374. buf.copy_from_slice(&vec);
  375. buf
  376. }
  377. }
  378. /// Module containing functions for serializing and deserializing buffers in `SectoredCursor<T>`.
  379. mod serde_cursor {
  380. use super::FromVec;
  381. use serde::{Deserialize, Deserializer, Serialize, Serializer};
  382. use std::{
  383. cell::RefCell,
  384. io::{Cursor, Read},
  385. result::Result,
  386. };
  387. pub fn serialize<T: AsRef<[u8]>, S: Serializer>(
  388. cursor: &RefCell<Cursor<T>>,
  389. ser: S,
  390. ) -> Result<S::Ok, S::Error> {
  391. let mut cursor = cursor.borrow_mut();
  392. let pos = cursor.position();
  393. cursor.set_position(0);
  394. let mut data = Vec::new();
  395. cursor
  396. .read_to_end(&mut data)
  397. .expect("reading from cursor failed");
  398. cursor.set_position(pos);
  399. data.serialize(ser)
  400. }
  401. pub fn deserialize<'de, T: FromVec, D: Deserializer<'de>>(
  402. de: D,
  403. ) -> Result<RefCell<Cursor<T>>, D::Error> {
  404. let data = Vec::<u8>::deserialize(de)?;
  405. Ok(RefCell::new(Cursor::new(T::from_vec(data))))
  406. }
  407. }
  408. /// A wrapper for `Cursor<T>` which implements additional traits.
  409. #[derive(Debug, PartialEq, Serialize, Deserialize)]
  410. pub struct BtCursor<T: FromVec> {
  411. #[serde(with = "serde_cursor")]
  412. cursor: RefCell<Cursor<T>>,
  413. }
  414. impl<T: FromVec> BtCursor<T> {
  415. pub fn new(inner: T) -> BtCursor<T> {
  416. BtCursor {
  417. cursor: RefCell::new(Cursor::new(inner)),
  418. }
  419. }
  420. }
  421. impl<T: FromVec> Decompose<T> for BtCursor<T> {
  422. fn into_inner(self) -> T {
  423. self.cursor.into_inner().into_inner()
  424. }
  425. }
  426. impl<T: FromVec + AsMut<[u8]>> BtCursor<T> {
  427. /// Access the data in this cursor as a mutable slice of bytes. The callback which access the
  428. /// bytes is expected to return the number of bytes to advance the cursor position by.
  429. fn access_advance<F: FnOnce(&mut [u8]) -> u64>(&mut self, limit: usize, accessor: F) -> u64 {
  430. let mut cursor = self.cursor.borrow_mut();
  431. let pos = cursor.position();
  432. let byte_slice = cursor.get_mut().as_mut();
  433. let max = byte_slice.len().max(limit);
  434. let pos_usize: usize = pos.try_into().unwrap();
  435. let byte_slice = &mut byte_slice[pos_usize..max];
  436. let advance_by = accessor(byte_slice);
  437. cursor.set_position(pos + advance_by);
  438. advance_by
  439. }
  440. /// Gives a `accessor` access to a slice containing all of the data in this cursor.
  441. fn access_whole<U, F: FnOnce(&mut [u8]) -> U>(&mut self, accessor: F) -> U {
  442. let mut cursor = self.cursor.borrow_mut();
  443. let byte_slice = cursor.get_mut().as_mut();
  444. accessor(byte_slice)
  445. }
  446. fn write_from_impl(
  447. &mut self,
  448. f: &mut dyn FileReadWriteVolatile,
  449. count: usize,
  450. off: u64,
  451. ) -> io::Result<usize> {
  452. let read = self.access_advance(count, |byte_slice| {
  453. let slice = unsafe {
  454. FileVolatileSlice::from_raw_ptr(byte_slice.as_mut_ptr(), byte_slice.len())
  455. };
  456. let read = f.read_at_volatile(slice, off).expect("read failed");
  457. read.try_into().unwrap()
  458. });
  459. Ok(read.try_into().unwrap())
  460. }
  461. }
  462. impl Write for BtCursor<Vec<u8>> {
  463. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  464. self.cursor.get_mut().write(buf)
  465. }
  466. fn flush(&mut self) -> io::Result<()> {
  467. self.cursor.get_mut().flush()
  468. }
  469. }
  470. impl<const N: usize> Write for BtCursor<[u8; N]> {
  471. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  472. self.cursor.get_mut().write(buf)
  473. }
  474. fn flush(&mut self) -> io::Result<()> {
  475. self.cursor.get_mut().flush()
  476. }
  477. }
  478. impl WriteAt for BtCursor<Vec<u8>> {
  479. fn write_at(&mut self, pos: u64, buf: &[u8]) -> io::Result<usize> {
  480. let pos: usize = pos.try_into().box_err()?;
  481. let vec = self.cursor.get_mut().get_mut();
  482. let end = pos + buf.len();
  483. if end > vec.len() {
  484. vec.resize(end, 0);
  485. }
  486. let slice = &mut vec[pos..end];
  487. slice.copy_from_slice(buf);
  488. Ok(end - pos)
  489. }
  490. fn flush(&mut self) -> io::Result<()> {
  491. Ok(())
  492. }
  493. }
  494. impl<const N: usize> WriteAt for BtCursor<[u8; N]> {
  495. fn write_at(&mut self, pos: u64, buf: &[u8]) -> io::Result<usize> {
  496. let pos: usize = pos.try_into().box_err()?;
  497. let slice = self.cursor.get_mut().get_mut().as_mut();
  498. let end = pos + buf.len();
  499. let slice = &mut slice[pos..end];
  500. slice.copy_from_slice(buf);
  501. Ok(end - pos)
  502. }
  503. fn flush(&mut self) -> io::Result<()> {
  504. Ok(())
  505. }
  506. }
  507. impl<T: FromVec> Read for BtCursor<T> {
  508. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  509. self.cursor.get_mut().read(buf)
  510. }
  511. }
  512. impl<T: FromVec> ReadAt for BtCursor<T> {
  513. fn read_at(&self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
  514. let pos: usize = pos.try_into().box_err()?;
  515. let borrow = self.cursor.try_borrow().box_err()?;
  516. let slice: &[u8] = borrow.get_ref().as_ref();
  517. let end = slice.len().min(pos + buf.len());
  518. let buf = &mut buf[..(end - pos)];
  519. buf.copy_from_slice(&slice[pos..end]);
  520. Ok(end - pos)
  521. }
  522. }
  523. impl<T: FromVec> Seek for BtCursor<T> {
  524. fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
  525. self.cursor.get_mut().seek(pos)
  526. }
  527. }
  528. impl WriteInteg for BtCursor<Vec<u8>> {
  529. fn flush_integ(&mut self, _: &[u8]) -> io::Result<()> {
  530. Ok(())
  531. }
  532. }
  533. impl<const N: usize> WriteInteg for BtCursor<[u8; N]> {
  534. fn flush_integ(&mut self, _: &[u8]) -> io::Result<()> {
  535. Ok(())
  536. }
  537. }
  538. impl<T: AsMut<[u8]> + FromVec> FileReadWriteVolatile for BtCursor<T> {
  539. fn read_volatile(
  540. &mut self,
  541. slice: fuse_backend_rs::file_buf::FileVolatileSlice,
  542. ) -> io::Result<usize> {
  543. let read = self.access_advance(usize::MAX, |byte_slice| {
  544. let written = slice.as_volatile_slice().write(byte_slice, 0).unwrap();
  545. written.try_into().unwrap()
  546. });
  547. Ok(read.try_into().unwrap())
  548. }
  549. fn write_volatile(
  550. &mut self,
  551. slice: fuse_backend_rs::file_buf::FileVolatileSlice,
  552. ) -> io::Result<usize> {
  553. let written = self.access_advance(usize::MAX, |byte_slice| {
  554. let read = slice.as_volatile_slice().read(byte_slice, 0).unwrap();
  555. read.try_into().unwrap()
  556. });
  557. Ok(written.try_into().unwrap())
  558. }
  559. fn read_at_volatile(
  560. &mut self,
  561. slice: fuse_backend_rs::file_buf::FileVolatileSlice,
  562. offset: u64,
  563. ) -> io::Result<usize> {
  564. Ok(self.access_whole(|byte_slice| {
  565. let offset = offset.try_into().unwrap();
  566. let byte_slice = &mut byte_slice[offset..];
  567. slice.as_volatile_slice().write(byte_slice, 0).unwrap()
  568. }))
  569. }
  570. fn write_at_volatile(
  571. &mut self,
  572. slice: fuse_backend_rs::file_buf::FileVolatileSlice,
  573. offset: u64,
  574. ) -> io::Result<usize> {
  575. Ok(self.access_whole(|byte_slice| {
  576. let offset = offset.try_into().unwrap();
  577. let byte_slice = &mut byte_slice[offset..];
  578. slice.as_volatile_slice().read(byte_slice, 0).unwrap()
  579. }))
  580. }
  581. }
  582. impl<T: AsMut<[u8]> + FromVec> ZeroCopyReader for BtCursor<T> {
  583. fn read_to(
  584. &mut self,
  585. f: &mut dyn FileReadWriteVolatile,
  586. count: usize,
  587. off: u64,
  588. ) -> io::Result<usize> {
  589. let written = self.access_advance(count, |byte_slice| {
  590. // Safety: The mut borrow of cursor is guaranteed to be the only one in existence
  591. // because this method takes &mut self.
  592. let slice = unsafe {
  593. FileVolatileSlice::from_raw_ptr(byte_slice.as_mut_ptr(), byte_slice.len())
  594. };
  595. let written = f.write_at_volatile(slice, off).expect("write failed");
  596. let written_u64: u64 = written.try_into().unwrap();
  597. written_u64
  598. });
  599. Ok(written.try_into().unwrap())
  600. }
  601. }
  602. impl ZeroCopyWriter for BtCursor<Vec<u8>> {
  603. fn write_from(
  604. &mut self,
  605. f: &mut dyn FileReadWriteVolatile,
  606. count: usize,
  607. off: u64,
  608. ) -> io::Result<usize> {
  609. self.write_from_impl(f, count, off)
  610. }
  611. }
  612. impl<const N: usize> ZeroCopyWriter for BtCursor<[u8; N]> {
  613. fn write_from(
  614. &mut self,
  615. f: &mut dyn FileReadWriteVolatile,
  616. count: usize,
  617. off: u64,
  618. ) -> io::Result<usize> {
  619. self.write_from_impl(f, count, off)
  620. }
  621. }
  622. impl<T: FromVec> Size for BtCursor<T> {
  623. fn size(&self) -> io::Result<Option<u64>> {
  624. let borrow = self.cursor.borrow();
  625. let slice: &[u8] = borrow.get_ref().as_ref();
  626. Ok(Some(slice.len() as u64))
  627. }
  628. }
  629. #[derive(Debug, PartialEq, Serialize, Deserialize)]
  630. pub struct SectoredCursor<T: FromVec> {
  631. cursor: BtCursor<T>,
  632. sect_sz: usize,
  633. meta: BlockMeta,
  634. require_sect_sz: bool,
  635. }
  636. impl<T: FromVec> SectoredCursor<T> {
  637. pub fn new(inner: T, sect_sz: usize) -> SectoredCursor<T> {
  638. SectoredCursor {
  639. cursor: BtCursor::new(inner),
  640. sect_sz,
  641. meta: BlockMeta::new(&*NODE_CREDS).unwrap(),
  642. require_sect_sz: true,
  643. }
  644. }
  645. pub fn require_sect_sz(mut self, require: bool) -> Self {
  646. self.require_sect_sz = require;
  647. self
  648. }
  649. }
  650. impl<T: FromVec> Sectored for SectoredCursor<T> {
  651. fn sector_sz(&self) -> usize {
  652. self.sect_sz
  653. }
  654. }
  655. impl<T: FromVec> Decompose<T> for SectoredCursor<T> {
  656. fn into_inner(self) -> T {
  657. self.cursor.into_inner()
  658. }
  659. }
  660. impl Write for SectoredCursor<Vec<u8>> {
  661. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  662. if self.require_sect_sz {
  663. self.assert_sector_sz(buf.len())?;
  664. }
  665. self.cursor.write(buf)
  666. }
  667. fn flush(&mut self) -> io::Result<()> {
  668. Write::flush(&mut self.cursor)
  669. }
  670. }
  671. impl<const N: usize> Write for SectoredCursor<[u8; N]> {
  672. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  673. if self.require_sect_sz {
  674. self.assert_sector_sz(buf.len())?;
  675. }
  676. self.cursor.write(buf)
  677. }
  678. fn flush(&mut self) -> io::Result<()> {
  679. Write::flush(&mut self.cursor)
  680. }
  681. }
  682. impl WriteAt for SectoredCursor<Vec<u8>> {
  683. fn write_at(&mut self, pos: u64, buf: &[u8]) -> io::Result<usize> {
  684. self.cursor.write_at(pos, buf)
  685. }
  686. fn flush(&mut self) -> io::Result<()> {
  687. Write::flush(&mut self.cursor)
  688. }
  689. }
  690. impl<const N: usize> WriteAt for SectoredCursor<[u8; N]> {
  691. fn write_at(&mut self, pos: u64, buf: &[u8]) -> io::Result<usize> {
  692. self.cursor.write_at(pos, buf)
  693. }
  694. fn flush(&mut self) -> io::Result<()> {
  695. Write::flush(&mut self.cursor)
  696. }
  697. }
  698. impl<T: FromVec> SectoredCursor<T> {
  699. fn flush_integ_impl(&mut self, integrity: &[u8]) -> io::Result<()> {
  700. let dest = self
  701. .meta
  702. .body
  703. .integrity
  704. .get_or_insert_with(<VarHash as Default>::default);
  705. dest.as_mut_slice().copy_from_slice(integrity);
  706. Ok(())
  707. }
  708. }
  709. impl WriteInteg for SectoredCursor<Vec<u8>> {
  710. fn flush_integ(&mut self, integrity: &[u8]) -> io::Result<()> {
  711. self.flush_integ_impl(integrity)
  712. }
  713. }
  714. impl<const N: usize> WriteInteg for SectoredCursor<[u8; N]> {
  715. fn flush_integ(&mut self, integrity: &[u8]) -> io::Result<()> {
  716. self.flush_integ_impl(integrity)
  717. }
  718. }
  719. impl<T: FromVec> Read for SectoredCursor<T> {
  720. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  721. if self.require_sect_sz {
  722. self.assert_sector_sz(buf.len())?;
  723. }
  724. self.cursor.read(buf)
  725. }
  726. }
  727. impl<T: FromVec> ReadAt for SectoredCursor<T> {
  728. fn read_at(&self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
  729. if self.require_sect_sz {
  730. self.assert_sector_sz(buf.len())?;
  731. }
  732. self.cursor.read_at(pos, buf)
  733. }
  734. }
  735. impl<T: FromVec> Seek for SectoredCursor<T> {
  736. fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
  737. self.cursor.seek(pos)
  738. }
  739. }
  740. impl<T: FromVec> AsRef<BlockMeta> for SectoredCursor<T> {
  741. fn as_ref(&self) -> &BlockMeta {
  742. &self.meta
  743. }
  744. }
  745. impl<T: FromVec> AsMut<BlockMeta> for SectoredCursor<T> {
  746. fn as_mut(&mut self) -> &mut BlockMeta {
  747. &mut self.meta
  748. }
  749. }
  750. impl<T: FromVec> Size for SectoredCursor<T> {
  751. fn size(&self) -> io::Result<Option<u64>> {
  752. self.cursor.size()
  753. }
  754. }