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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#[macro_use]
extern crate sgx_types;
use libc::{self, c_void};
use sgx_types::error::OsResult;
use std::ffi::{CStr, CString};
use std::fs::{self, File, OpenOptions};
use std::io::{Error, ErrorKind, SeekFrom};
use std::mem::{self, ManuallyDrop};
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::path::Path;
const MILISECONDS_SLEEP_FOPEN: u32 = 10;
const MAX_FOPEN_RETRIES: usize = 10;
const NODE_SIZE: usize = 4096;
const RECOVERY_NODE_SIZE: usize = mem::size_of::<u64>() + NODE_SIZE;
#[derive(Debug)]
pub struct HostFile {
stream: FileStream,
fd: RawFd,
}
impl HostFile {
pub fn open(name: &Path, readonly: bool) -> OsResult<HostFile> {
let mut open_mode = OpenOptions::new();
open_mode.read(true);
if !readonly {
open_mode.write(true).create(true);
}
let oflag = libc::O_LARGEFILE;
let mode = libc::S_IRUSR
| libc::S_IWUSR
| libc::S_IRGRP
| libc::S_IWGRP
| libc::S_IROTH
| libc::S_IWOTH;
let file = open_mode
.mode(mode)
.custom_flags(oflag)
.open(name)
.map_err(|e| e.raw_os_error().unwrap_or(libc::EIO))?;
let op = if readonly {
libc::LOCK_SH
} else {
libc::LOCK_EX
} | libc::LOCK_NB; let fd = file.as_raw_fd();
unsafe {
if libc::flock(fd, op) < 0 {
Err(errno())
} else {
Ok(())
}
}?;
let mode = CStr::from_bytes_with_nul(if readonly { b"rb\0" } else { b"r+b\0" })
.map_err(|_| libc::EINVAL)?;
let stream = unsafe {
FileStream::from_raw_fd(fd, mode).map_err(|e| {
libc::flock(fd, libc::LOCK_UN);
e
})
}?;
Ok(HostFile {
stream,
fd: file.into_raw_fd(),
})
}
pub fn read(&mut self, number: u64, node: &mut [u8]) -> OsResult {
ensure!(node.len() == NODE_SIZE, libc::EINVAL);
let offset = number * NODE_SIZE as u64;
self.stream.seek(SeekFrom::Start(offset))?;
self.stream.read(node)
}
pub fn write(&mut self, number: u64, node: &[u8]) -> OsResult {
ensure!(node.len() == NODE_SIZE, libc::EINVAL);
let offset = number * NODE_SIZE as u64;
self.stream.seek(SeekFrom::Start(offset))?;
self.stream.write(node)
}
pub fn flush(&mut self) -> OsResult {
self.stream.flush()
}
pub fn size(&self) -> OsResult<usize> {
let file = ManuallyDrop::new(unsafe { File::from_raw_fd(self.fd) });
let metadata = file
.metadata()
.map_err(|e| e.raw_os_error().unwrap_or(libc::EIO))?;
ensure!(metadata.is_file(), libc::EINVAL);
Ok(metadata.len() as usize)
}
pub fn into_raw_stream(self) -> RawFileStream {
ManuallyDrop::new(self).stream.stream
}
pub unsafe fn from_raw_stream(stream: RawFileStream) -> OsResult<HostFile> {
ensure!(!stream.is_null(), libc::EINVAL);
let fd = libc::fileno(stream);
ensure!(fd >= 0, errno());
Ok(Self {
stream: FileStream { stream },
fd,
})
}
}
impl Drop for HostFile {
fn drop(&mut self) {
unsafe {
libc::flock(self.fd, libc::LOCK_UN);
}
}
}
#[derive(Debug)]
pub struct RecoveryFile {
stream: FileStream,
}
impl RecoveryFile {
pub fn open(name: &Path) -> OsResult<RecoveryFile> {
let mode = CStr::from_bytes_with_nul(b"wb\0").map_err(|_| libc::EINVAL)?;
for _ in 0..MAX_FOPEN_RETRIES {
if let Ok(stream) = FileStream::open(name, mode) {
return Ok(RecoveryFile { stream });
}
unsafe { libc::usleep(MILISECONDS_SLEEP_FOPEN) };
}
Err(libc::EBUSY)
}
pub fn write(&mut self, node: &[u8]) -> OsResult {
ensure!(node.len() == RECOVERY_NODE_SIZE, libc::EINVAL);
self.stream.write(node)
}
pub fn into_raw_stream(self) -> RawFileStream {
ManuallyDrop::new(self).stream.stream
}
pub unsafe fn from_raw_stream(stream: RawFileStream) -> OsResult<RecoveryFile> {
Ok(Self {
stream: FileStream { stream },
})
}
}
pub type RawFileStream = *mut libc::FILE;
fn cstr(name: &Path) -> OsResult<CString> {
CString::new(name.to_str().ok_or(libc::EINVAL)?).map_err(|_| libc::EINVAL)
}
fn errno() -> i32 {
Error::last_os_error().raw_os_error().unwrap_or(0)
}
#[derive(Debug)]
struct FileStream {
stream: RawFileStream,
}
impl FileStream {
fn open(name: &Path, mode: &CStr) -> OsResult<FileStream> {
let name = cstr(name)?;
let stream = unsafe { libc::fopen(name.as_ptr(), mode.as_ptr()) };
if stream.is_null() {
Err(errno())
} else {
Ok(FileStream { stream })
}
}
fn read(&self, buf: &mut [u8]) -> OsResult {
let size =
unsafe { libc::fread(buf.as_mut_ptr() as *mut c_void, buf.len(), 1, self.stream) };
if size != 1 {
let err = self.last_error();
if err != 0 {
bail!(err);
} else if errno() != 0 {
bail!(errno());
} else {
bail!(libc::EIO);
}
}
Ok(())
}
fn write(&self, buf: &[u8]) -> OsResult {
let size =
unsafe { libc::fwrite(buf.as_ptr() as *const c_void, 1, buf.len(), self.stream) };
if size != buf.len() {
let err = self.last_error();
if err != 0 {
bail!(err);
} else if errno() != 0 {
bail!(errno());
} else {
bail!(libc::EIO);
}
}
Ok(())
}
fn flush(&self) -> OsResult {
if unsafe { libc::fflush(self.stream) } != 0 {
bail!(errno())
}
Ok(())
}
fn seek(&self, pos: SeekFrom) -> OsResult {
let (offset, whence) = match pos {
SeekFrom::Start(off) => (off as i64, libc::SEEK_SET),
SeekFrom::End(off) => (off, libc::SEEK_END),
SeekFrom::Current(off) => (off, libc::SEEK_CUR),
};
if unsafe { libc::fseeko(self.stream, offset, whence) } != 0 {
bail!(errno())
}
Ok(())
}
fn tell(&self) -> OsResult<u64> {
let off = unsafe { libc::ftello(self.stream) };
ensure!(off >= 0, errno());
Ok(off as u64)
}
fn last_error(&self) -> i32 {
unsafe { libc::ferror(self.stream) }
}
unsafe fn from_raw_fd(fd: RawFd, mode: &CStr) -> OsResult<FileStream> {
let stream = libc::fdopen(fd, mode.as_ptr());
ensure!(!stream.is_null(), errno());
Ok(FileStream { stream })
}
}
impl Drop for FileStream {
fn drop(&mut self) {
let _ = unsafe { libc::fclose(self.stream) };
}
}
pub fn remove(name: &Path) -> OsResult {
fs::remove_file(name).map_err(|e| e.raw_os_error().unwrap_or(libc::EIO))
}
pub fn try_exists(path: &Path) -> OsResult<bool> {
match fs::metadata(path) {
Ok(_) => Ok(true),
Err(error) if error.kind() == ErrorKind::NotFound => Ok(false),
Err(error) => Err(error.raw_os_error().unwrap_or(libc::EIO)),
}
}
pub fn recovery(source: &Path, recovery: &Path) -> OsResult {
let mode = CStr::from_bytes_with_nul(b"rb\0").map_err(|_| libc::EINVAL)?;
let recov = FileStream::open(recovery, mode)?;
recov.seek(SeekFrom::End(0))?;
let size = recov.tell()? as usize;
recov.seek(SeekFrom::Start(0))?;
ensure!(size % RECOVERY_NODE_SIZE == 0, libc::ENOTSUP);
let nodes_count = size / RECOVERY_NODE_SIZE;
let mode = CStr::from_bytes_with_nul(b"r+b\0").map_err(|_| libc::EINVAL)?;
let src = FileStream::open(source, mode)?;
let mut data = vec![0_u8; RECOVERY_NODE_SIZE];
for _ in 0..nodes_count {
recov.read(data.as_mut_slice())?;
let mut number = [0u8; 8];
number.copy_from_slice(&data[0..8]);
let physical_node_number = u64::from_ne_bytes(number);
src.seek(SeekFrom::Start(physical_node_number * NODE_SIZE as u64))?;
src.write(&data[8..])?;
}
src.flush()?;
remove(recovery)
}