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> { fn write_at(&self, buf: &[u8], offset: usize) -> DevResult<usize> {
match file_write_at(self.fd, offset, buf) { match file_write_at(self.fd, offset, buf) {
size if size > 0 => Ok(size as usize), size if size > 0 => Ok(size as usize),
_ => panic!(), e => panic!("write_at {}", e),
} }
} }
fn set_len(&self, len: usize) -> DevResult<()> { fn set_len(&self, len: usize) -> DevResult<()> {
match file_set_len(self.fd, len) { match file_set_len(self.fd, len) {
0 => Ok(()), 0 => Ok(()),
_ => panic!(), e => panic!("set_len {}", e),
} }
} }
fn flush(&self) -> DevResult<()> { fn flush(&self) -> DevResult<()> {
match file_flush(self.fd) { match file_flush(self.fd) {
0 => Ok(()), 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 { macro_rules! try_io {
($expr:expr) => (match $expr { ($expr:expr) => (match $expr {
Ok(val) => val, 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); debug!("set_len fd = {}, len = {}", fd, len);
let current_len = try_io!(file.seek(SeekFrom::End(0))) as usize; let current_len = try_io!(file.seek(SeekFrom::End(0))) as usize;
if current_len < len { if current_len < len {
let mut zeros = Vec::<u8>::new(); static ZEROS: [u8; 0x1000] = [0; 0x1000];
zeros.resize(len - current_len, 0); let mut rest_len = len - current_len;
try_io!(file.write(zeros.as_slice())); 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? // TODO: how to shrink a file?
0 0

Loading…
Cancel
Save