|
|
@ -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
|
|
|
|