1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#[cfg(windows)]
use std::sync::Mutex;
use super::*;
#[cfg(unix)]
use std::os::unix::fs::FileExt;
#[cfg(windows)]
use std::os::windows::fs::FileExt;
/// Multithreaded IO support for Files
pub(crate) trait Pio {
/// Read from a specific offset without changing
/// the underlying file offset.
fn pread_exact(
&self,
to_buf: &mut [u8],
offset: LogId,
) -> io::Result<()>;
/// Write to a specific offset without changing
/// the underlying file offset.
fn pwrite_all(
&self,
from_buf: &[u8],
offset: LogId,
) -> io::Result<()>;
}
// On systems that support pread/pwrite, use them underneath.
#[cfg(unix)]
impl Pio for std::fs::File {
fn pread_exact(
&self,
mut buf: &mut [u8],
mut offset: LogId,
) -> io::Result<()> {
while !buf.is_empty() {
match self.read_at(buf, offset) {
Ok(0) => break,
Ok(n) => {
offset += n as LogId;
let tmp = buf;
buf = &mut tmp[n..];
}
Err(ref e)
if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
if !buf.is_empty() {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
))
} else {
Ok(())
}
}
fn pwrite_all(
&self,
mut buf: &[u8],
mut offset: LogId,
) -> io::Result<()> {
while !buf.is_empty() {
match self.write_at(buf, offset) {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::WriteZero,
"failed to write whole buffer",
))
}
Ok(n) => {
offset += n as LogId;
buf = &buf[n..]
}
Err(ref e)
if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(())
}
}
// HACK HACK HACK get this working with real parallel IO
#[cfg(windows)]
lazy_static! {
pub(crate) static ref GLOBAL_FILE_LOCK: Mutex<()> =
Mutex::new(());
}
#[cfg(windows)]
impl Pio for std::fs::File {
fn pread_exact(
&self,
mut buf: &mut [u8],
mut offset: LogId,
) -> io::Result<()> {
// HACK HACK HACK get this working with real parallel IO
let _lock = GLOBAL_FILE_LOCK.lock().unwrap();
while !buf.is_empty() {
match self.seek_read(buf, offset) {
Ok(0) => break,
Ok(n) => {
offset += n as LogId;
let tmp = buf;
buf = &mut tmp[n..];
}
Err(ref e)
if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
if !buf.is_empty() {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
))
} else {
Ok(())
}
}
fn pwrite_all(
&self,
mut buf: &[u8],
mut offset: LogId,
) -> io::Result<()> {
// HACK HACK HACK get this working with real parallel IO
let _lock = GLOBAL_FILE_LOCK.lock().unwrap();
while !buf.is_empty() {
match self.seek_write(buf, offset) {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::WriteZero,
"failed to write whole buffer",
))
}
Ok(n) => {
offset += n as LogId;
buf = &buf[n..]
}
Err(ref e)
if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(())
}
}