fix strange bug in sgx set_len

master
WangRunji 6 years ago
parent 9a89ed174c
commit 9b475b0efd

@ -59,21 +59,21 @@ impl File for SgxFile {
fn write_at(&self, buf: &[u8], offset: usize) -> DevResult<usize> {
match file_write_at(self.fd, offset, buf) {
size if size > 0 => Ok(size as usize),
_ => panic!(),
e => panic!("write_at {}", e),
}
}
fn set_len(&self, len: usize) -> DevResult<()> {
match file_set_len(self.fd, len) {
0 => Ok(()),
_ => panic!(),
e => panic!("set_len {}", e),
}
}
fn flush(&self) -> DevResult<()> {
match file_flush(self.fd) {
0 => Ok(()),
_ => panic!(),
e => panic!("flush {}", e),
}
}
}

@ -64,7 +64,10 @@ pub extern "C" fn ecall_set_sefs_dir(path: *const u8, len: usize) -> i32 {
macro_rules! try_io {
($expr:expr) => (match $expr {
Ok(val) => val,
Err(err) => return err.raw_os_error().unwrap(),
Err(err) => {
error!("{:?}", err);
return err.raw_os_error().unwrap();
}
});
}
@ -140,9 +143,15 @@ pub extern "C" fn ecall_file_set_len(fd: usize, len: usize) -> i32 {
debug!("set_len fd = {}, len = {}", fd, len);
let current_len = try_io!(file.seek(SeekFrom::End(0))) as usize;
if current_len < len {
let mut zeros = Vec::<u8>::new();
zeros.resize(len - current_len, 0);
try_io!(file.write(zeros.as_slice()));
static ZEROS: [u8; 0x1000] = [0; 0x1000];
let mut rest_len = len - current_len;
while rest_len != 0 {
let l = rest_len.min(0x1000);
try_io!(file.write(&ZEROS[..l]));
rest_len -= l;
}
// NOTE: Don't try to write a large slice at once.
// It will cause Error 12: "Cannot allocate memory"
}
// TODO: how to shrink a file?
0

Loading…
Cancel
Save