22// SPDX-FileCopyrightText: Copyright the Vortex contributors
33
44use std:: fs:: File ;
5+ #[ cfg( not( unix) ) ]
6+ use std:: io:: { Read , Seek } ;
7+ #[ cfg( unix) ]
58use std:: os:: unix:: fs:: FileExt ;
9+ #[ cfg( windows) ]
10+ use std:: os:: windows:: fs:: FileExt ;
611use std:: path:: { Path , PathBuf } ;
712use std:: sync:: Arc ;
813
@@ -15,6 +20,23 @@ use vortex_error::{VortexError, VortexResult};
1520use crate :: file:: { CoalesceWindow , IntoReadSource , IoRequest , ReadSource , ReadSourceRef } ;
1621use crate :: runtime:: Handle ;
1722
23+ /// Read exactly `buffer.len()` bytes from `file` starting at `offset`.
24+ /// This is a platform-specific helper that uses the most efficient method available.
25+ #[ cfg( not( target_arch = "wasm32" ) ) ]
26+ pub ( crate ) fn read_exact_at ( file : & File , buffer : & mut [ u8 ] , offset : u64 ) -> std:: io:: Result < ( ) > {
27+ #[ cfg( unix) ]
28+ {
29+ file. read_exact_at ( buffer, offset)
30+ }
31+ #[ cfg( not( unix) ) ]
32+ {
33+ use std:: io:: SeekFrom ;
34+ let mut file_ref = file;
35+ file_ref. seek ( SeekFrom :: Start ( offset) ) ?;
36+ file_ref. read_exact ( buffer)
37+ }
38+ }
39+
1840const COALESCING_WINDOW : CoalesceWindow = CoalesceWindow {
1941 // TODO(ngates): these numbers don't make sense if we're using spawn_blocking..
2042 distance : 8 * 1024 , // KB
@@ -83,12 +105,7 @@ impl ReadSource for FileIoSource {
83105 let mut buffer = ByteBufferMut :: with_capacity_aligned ( len, req. alignment ( ) ) ;
84106 unsafe { buffer. set_len ( len) } ;
85107
86- #[ cfg( unix) ]
87- let buffer_res = file. read_exact_at ( & mut buffer, offset) ;
88- #[ cfg( not( unix) ) ]
89- let buffer_res = file
90- . seek ( io:: SeekFrom :: Start ( offset) )
91- . and_then ( |_| file. read_exact ( & mut buffer) ) ;
108+ let buffer_res = read_exact_at ( & file, & mut buffer, offset) ;
92109
93110 req. resolve (
94111 buffer_res
0 commit comments