1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Utilities that handle persisting Rust-Lightning data to disk via standard filesystem APIs.
11 use alloc::str::FromStr;
12 use core::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16 #[cfg(feature="no-std")]
17 use alloc::{vec::Vec, boxed::Box};
21 use alloc::str::FromStr;
22 use core::ffi::c_void;
23 use core::convert::Infallible;
24 use bitcoin::hashes::Hash;
25 use crate::c_types::*;
26 #[cfg(feature="no-std")]
27 use alloc::{vec::Vec, boxed::Box};
31 use lightning_persister::FilesystemPersister as nativeFilesystemPersisterImport;
32 pub(crate) type nativeFilesystemPersister = nativeFilesystemPersisterImport;
34 /// FilesystemPersister persists channel data on disk, where each channel's
35 /// data is stored in a file named after its funding outpoint.
37 /// Warning: this module does the best it can with calls to persist data, but it
38 /// can only guarantee that the data is passed to the drive. It is up to the
39 /// drive manufacturers to do the actual persistence properly, which they often
40 /// don't (especially on consumer-grade hardware). Therefore, it is up to the
41 /// user to validate their entire storage stack, to ensure the writes are
43 /// Corollary: especially when dealing with larger amounts of money, it is best
44 /// practice to have multiple channel data backups and not rely only on one
45 /// FilesystemPersister.
48 pub struct FilesystemPersister {
49 /// A pointer to the opaque Rust object.
51 /// Nearly everywhere, inner must be non-null, however in places where
52 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
53 pub inner: *mut nativeFilesystemPersister,
54 /// Indicates that this is the only struct which contains the same pointer.
56 /// Rust functions which take ownership of an object provided via an argument require
57 /// this to be true and invalidate the object pointed to by inner.
61 impl Drop for FilesystemPersister {
63 if self.is_owned && !<*mut nativeFilesystemPersister>::is_null(self.inner) {
64 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
68 /// Frees any resources used by the FilesystemPersister, if is_owned is set and inner is non-NULL.
70 pub extern "C" fn FilesystemPersister_free(this_obj: FilesystemPersister) { }
72 /// Used only if an object of this type is returned as a trait impl by a method
73 pub(crate) extern "C" fn FilesystemPersister_free_void(this_ptr: *mut c_void) {
74 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeFilesystemPersister); }
77 impl FilesystemPersister {
78 pub(crate) fn get_native_ref(&self) -> &'static nativeFilesystemPersister {
79 unsafe { &*ObjOps::untweak_ptr(self.inner) }
81 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFilesystemPersister {
82 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
84 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
85 pub(crate) fn take_inner(mut self) -> *mut nativeFilesystemPersister {
86 assert!(self.is_owned);
87 let ret = ObjOps::untweak_ptr(self.inner);
88 self.inner = core::ptr::null_mut();
92 /// Initialize a new FilesystemPersister and set the path to the individual channels'
96 pub extern "C" fn FilesystemPersister_new(mut path_to_channel_data: crate::c_types::Str) -> crate::lightning_persister::FilesystemPersister {
97 let mut ret = lightning_persister::FilesystemPersister::new(path_to_channel_data.into_string());
98 crate::lightning_persister::FilesystemPersister { inner: ObjOps::heap_alloc(ret), is_owned: true }
101 /// Get the directory which was provided when this persister was initialized.
104 pub extern "C" fn FilesystemPersister_get_data_dir(this_arg: &crate::lightning_persister::FilesystemPersister) -> crate::c_types::Str {
105 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_data_dir();
109 /// Read `ChannelMonitor`s from disk.
112 pub extern "C" fn FilesystemPersister_read_channelmonitors(this_arg: &crate::lightning_persister::FilesystemPersister, mut keys_manager: crate::lightning::chain::keysinterface::KeysInterface) -> crate::c_types::derived::CResult_CVec_C2Tuple_BlockHashChannelMonitorZZErrorZ {
113 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.read_channelmonitors(keys_manager);
114 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let (mut orig_ret_0_0_0, mut orig_ret_0_0_1) = item; let mut local_ret_0_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_0.into_inner() }, crate::lightning::chain::channelmonitor::ChannelMonitor { inner: ObjOps::heap_alloc(orig_ret_0_0_1), is_owned: true }).into(); local_ret_0_0 }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::IOError::from_rust(e) }).into() };