diff --git a/runtime/src/append_vec.rs b/runtime/src/append_vec.rs index 1f5c6639b3..4eae2ade86 100644 --- a/runtime/src/append_vec.rs +++ b/runtime/src/append_vec.rs @@ -321,32 +321,7 @@ impl AppendVec { } pub fn new_from_file>(path: P, current_len: usize) -> io::Result<(Self, usize)> { - let data = OpenOptions::new() - .read(true) - .write(true) - .create(false) - .open(&path)?; - - let file_size = std::fs::metadata(&path)?.len(); - AppendVec::sanitize_len_and_size(current_len, file_size as usize)?; - - let map = unsafe { - let result = MmapMut::map_mut(&data); - if result.is_err() { - // for vm.max_map_count, error is: {code: 12, kind: Other, message: "Cannot allocate memory"} - info!("memory map error: {:?}. This may be because vm.max_map_count is not set correctly.", result); - } - result? - }; - - let new = AppendVec { - path: path.as_ref().to_path_buf(), - map, - append_lock: Mutex::new(()), - current_len: AtomicUsize::new(current_len), - file_size, - remove_on_drop: true, - }; + let new = Self::new_from_file_unchecked(path, current_len)?; let (sanitized, num_accounts) = new.sanitize_layout_and_length(); if !sanitized { @@ -359,6 +334,39 @@ impl AppendVec { Ok((new, num_accounts)) } + /// Creates an appendvec from file without performing sanitize checks or counting the number of accounts + pub fn new_from_file_unchecked>( + path: P, + current_len: usize, + ) -> io::Result { + let file_size = std::fs::metadata(&path)?.len(); + Self::sanitize_len_and_size(current_len, file_size as usize)?; + + let data = OpenOptions::new() + .read(true) + .write(true) + .create(false) + .open(&path)?; + + let map = unsafe { + let result = MmapMut::map_mut(&data); + if result.is_err() { + // for vm.max_map_count, error is: {code: 12, kind: Other, message: "Cannot allocate memory"} + info!("memory map error: {:?}. This may be because vm.max_map_count is not set correctly.", result); + } + result? + }; + + Ok(AppendVec { + path: path.as_ref().to_path_buf(), + map, + append_lock: Mutex::new(()), + current_len: AtomicUsize::new(current_len), + file_size, + remove_on_drop: true, + }) + } + fn sanitize_layout_and_length(&self) -> (bool, usize) { let mut offset = 0;