vfs: implement file truncation, bump open file limit

This commit is contained in:
Mark Poliakov 2025-02-24 10:59:48 +02:00
parent a1ccdf7e76
commit f1a6033f5b
4 changed files with 30 additions and 8 deletions

View File

@ -357,6 +357,17 @@ impl File {
}
}
/// Resizes this file to match the requested `size`
pub fn truncate(&self, size: u64) -> Result<(), Error> {
match &self.inner {
FileInner::Regular(file) => file.truncate(size),
_ => {
log::error!("TODO: truncate operation on unsupported file type");
Err(Error::InvalidOperation)
}
}
}
/// Interprets the file as a poll channel
pub fn as_poll_channel(&self) -> Result<&FdPoll, Error> {
if let FileInner::Poll(poll) = &self.inner {
@ -586,6 +597,8 @@ impl<T: TerminalHalf> TerminalHalfWrapper<T> {
}
impl FileSet {
const MAX_FILES: usize = 256;
/// Creates an empty [FileSet]
pub fn new() -> Self {
Self {
@ -610,7 +623,7 @@ impl FileSet {
/// Associates a `file` with any available [RawFd] and returns it
pub fn place_file(&mut self, file: FileRef, skip_stdio: bool) -> Result<RawFd, Error> {
let start = if skip_stdio { 3 } else { 0 };
for idx in start..64 {
for idx in start..Self::MAX_FILES as u32 {
let fd = RawFd::from(idx);
if let Entry::Vacant(e) = self.map.entry(fd) {

View File

@ -52,6 +52,14 @@ impl RegularFile {
reg.write(&self.node, self.instance_data.as_ref(), pos, buf)
}
pub fn truncate(&self, size: u64) -> Result<(), Error> {
if !self.write {
return Err(Error::InvalidFile);
}
let reg = self.node.as_regular()?;
reg.truncate(&self.node, size)
}
// TODO should seek beyond the end of a read-only file be allowed?
pub fn seek(&self, from: SeekFrom) -> Result<u64, Error> {
let mut position = self.position.lock();

View File

@ -269,11 +269,11 @@ impl Node {
// Try to fetch the size from the cache
let mut props = self.props.lock();
if let Some(size) = props.size {
return Ok(size);
}
if self.flags.contains(NodeFlags::IN_MEMORY_SIZE) {
if let Some(size) = props.size {
return Ok(size);
}
if let Ok(dir) = self.as_directory() {
return Ok(dir.children.lock().len() as _);
}

View File

@ -97,9 +97,10 @@ pub(crate) fn read(fd: RawFd, buffer: &mut [u8]) -> Result<usize, Error> {
}
pub(crate) fn truncate(fd: RawFd, size: u64) -> Result<(), Error> {
let _ = fd;
let _ = size;
todo!()
let thread = Thread::current();
let process = thread.process();
run_with_io(&process, |io| io.files.file(fd)?.truncate(size))
}
pub(crate) fn fsync(fd: RawFd, what: FileSync) -> Result<(), Error> {