fix sfs: auto resize when writing to end

master
WangRunji 6 years ago
parent def8615109
commit af02ecfb3f

@ -175,11 +175,6 @@ impl Filesystem for VfsFuse {
fn write(&mut self, _req: &Request, ino: u64, _fh: u64, offset: i64, data: &[u8], _flags: u32, reply: ReplyWrite) { fn write(&mut self, _req: &Request, ino: u64, _fh: u64, offset: i64, data: &[u8], _flags: u32, reply: ReplyWrite) {
let inode = try_vfs!(reply, self.get_inode(ino)); let inode = try_vfs!(reply, self.get_inode(ino));
let info = try_vfs!(reply, inode.metadata());
let end = offset as usize + data.len();
if end > info.size {
try_vfs!(reply, inode.resize(end));
}
let len = try_vfs!(reply, inode.write_at(offset as usize, data)); let len = try_vfs!(reply, inode.write_at(offset as usize, data));
reply.written(len as u32); reply.written(len as u32);
} }

@ -9,7 +9,6 @@ rcore-fs = { path = "../rcore-fs" }
static_assertions = "0.3" static_assertions = "0.3"
spin = "0.4" spin = "0.4"
log = "0.4" log = "0.4"
sgx_tstd = { path = "../../rust-sgx-sdk/sgx_tstd", optional = true }
[dependencies.bitvec] [dependencies.bitvec]
version = "0.9" version = "0.9"
@ -18,4 +17,3 @@ features = ["alloc"]
[features] [features]
std = ["rcore-fs/std"] std = ["rcore-fs/std"]
sgx = ["sgx_tstd"]

@ -1,7 +1,10 @@
#![no_std]
#![feature(alloc)] #![feature(alloc)]
#![feature(const_str_len)] #![feature(const_str_len)]
extern crate alloc; extern crate alloc;
#[macro_use]
extern crate log;
use alloc::{boxed::Box, collections::BTreeMap, string::String, sync::{Arc, Weak}, vec::Vec}; use alloc::{boxed::Box, collections::BTreeMap, string::String, sync::{Arc, Weak}, vec::Vec};
use core::any::Any; use core::any::Any;
@ -9,7 +12,6 @@ use core::fmt::{Debug, Error, Formatter};
use core::mem::uninitialized; use core::mem::uninitialized;
use bitvec::BitVec; use bitvec::BitVec;
use log::*;
use spin::RwLock; use spin::RwLock;
use rcore_fs::dev::Device; use rcore_fs::dev::Device;
@ -269,9 +271,15 @@ impl vfs::INode for INodeImpl {
self._read_at(offset, buf) self._read_at(offset, buf)
} }
fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> { fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
if self.disk_inode.read().type_!=FileType::File { let DiskINode { type_, size, .. } = **self.disk_inode.read();
if type_ != FileType::File {
return Err(FsError::NotFile); return Err(FsError::NotFile);
} }
// resize if not large enough
let end_offset = offset + buf.len();
if (size as usize) < end_offset {
self._resize(end_offset)?;
}
self._write_at(offset, buf) self._write_at(offset, buf)
} }
/// the size returned here is logical size(entry num for directory), not the disk space used. /// the size returned here is logical size(entry num for directory), not the disk space used.

Loading…
Cancel
Save