mirror of
https://github.com/rcore-os/rCore.git
synced 2024-11-23 08:26:17 +04:00
impl sys_lseek
This commit is contained in:
parent
70a950bf40
commit
4e146ec491
@ -7,7 +7,7 @@ use rcore_fs::vfs::{Metadata, INode, Result, FsError};
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct FileHandle {
|
pub struct FileHandle {
|
||||||
inode: Arc<INode>,
|
inode: Arc<INode>,
|
||||||
offset: usize,
|
offset: u64,
|
||||||
options: OpenOptions,
|
options: OpenOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,6 +19,13 @@ pub struct OpenOptions {
|
|||||||
pub append: bool,
|
pub append: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum SeekFrom {
|
||||||
|
Start(u64),
|
||||||
|
End(i64),
|
||||||
|
Current(i64),
|
||||||
|
}
|
||||||
|
|
||||||
impl FileHandle {
|
impl FileHandle {
|
||||||
pub fn new(inode: Arc<INode>, options: OpenOptions) -> Self {
|
pub fn new(inode: Arc<INode>, options: OpenOptions) -> Self {
|
||||||
FileHandle {
|
FileHandle {
|
||||||
@ -32,8 +39,8 @@ impl FileHandle {
|
|||||||
if !self.options.read {
|
if !self.options.read {
|
||||||
return Err(FsError::InvalidParam); // FIXME: => EBADF
|
return Err(FsError::InvalidParam); // FIXME: => EBADF
|
||||||
}
|
}
|
||||||
let len = self.inode.read_at(self.offset, buf)?;
|
let len = self.inode.read_at(self.offset as usize, buf)?;
|
||||||
self.offset += len;
|
self.offset += len as u64;
|
||||||
Ok(len)
|
Ok(len)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,13 +50,22 @@ impl FileHandle {
|
|||||||
}
|
}
|
||||||
if self.options.append {
|
if self.options.append {
|
||||||
let info = self.inode.metadata()?;
|
let info = self.inode.metadata()?;
|
||||||
self.offset = info.size;
|
self.offset = info.size as u64;
|
||||||
}
|
}
|
||||||
let len = self.inode.write_at(self.offset, buf)?;
|
let len = self.inode.write_at(self.offset as usize, buf)?;
|
||||||
self.offset += len;
|
self.offset += len as u64;
|
||||||
Ok(len)
|
Ok(len)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
|
||||||
|
self.offset = match pos {
|
||||||
|
SeekFrom::Start(offset) => offset,
|
||||||
|
SeekFrom::End(offset) => (self.inode.metadata()?.size as i64 + offset) as u64,
|
||||||
|
SeekFrom::Current(offset) => (self.offset as i64 + offset) as u64,
|
||||||
|
};
|
||||||
|
Ok(self.offset)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn info(&self) -> Result<Metadata> {
|
pub fn info(&self) -> Result<Metadata> {
|
||||||
self.inode.metadata()
|
self.inode.metadata()
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use crate::fs::{ROOT_INODE, OpenOptions};
|
use crate::fs::*;
|
||||||
use rcore_fs::vfs::Timespec;
|
use rcore_fs::vfs::Timespec;
|
||||||
|
|
||||||
pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
|
pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
|
||||||
@ -89,6 +89,21 @@ pub fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
|
|||||||
Ok(0)
|
Ok(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn sys_lseek(fd: usize, offset: i64, whence: u8) -> SysResult {
|
||||||
|
let pos = match whence {
|
||||||
|
SEEK_SET => SeekFrom::Start(offset as u64),
|
||||||
|
SEEK_END => SeekFrom::End(offset),
|
||||||
|
SEEK_CUR => SeekFrom::Current(offset),
|
||||||
|
_ => return Err(SysError::Inval),
|
||||||
|
};
|
||||||
|
info!("lseek: fd: {}, pos: {:?}", fd, pos);
|
||||||
|
|
||||||
|
let mut proc = process();
|
||||||
|
let file = get_file(&mut proc, fd)?;
|
||||||
|
let offset = file.seek(pos)?;
|
||||||
|
Ok(offset as isize)
|
||||||
|
}
|
||||||
|
|
||||||
/// entry_id = dentry.offset / 256
|
/// entry_id = dentry.offset / 256
|
||||||
/// dentry.name = entry_name
|
/// dentry.name = entry_name
|
||||||
/// dentry.offset += 256
|
/// dentry.offset += 256
|
||||||
@ -309,3 +324,7 @@ impl From<Metadata> for Stat {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SEEK_SET: u8 = 1;
|
||||||
|
const SEEK_CUR: u8 = 2;
|
||||||
|
const SEEK_END: u8 = 4;
|
||||||
|
@ -36,7 +36,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
|
|||||||
// 004 => sys_stat(),
|
// 004 => sys_stat(),
|
||||||
005 => sys_fstat(args[0], args[1] as *mut Stat),
|
005 => sys_fstat(args[0], args[1] as *mut Stat),
|
||||||
// 007 => sys_poll(),
|
// 007 => sys_poll(),
|
||||||
// 008 => sys_lseek(),
|
008 => sys_lseek(args[0], args[1] as i64, args[2] as u8),
|
||||||
009 => sys_mmap(args[0], args[1], MmapProt::from_bits_truncate(args[2]), MmapFlags::from_bits_truncate(args[3]), args[4] as i32, args[5]),
|
009 => sys_mmap(args[0], args[1], MmapProt::from_bits_truncate(args[2]), MmapFlags::from_bits_truncate(args[3]), args[4] as i32, args[5]),
|
||||||
011 => sys_munmap(args[0], args[1]),
|
011 => sys_munmap(args[0], args[1]),
|
||||||
// 019 => sys_readv(),
|
// 019 => sys_readv(),
|
||||||
|
Loading…
Reference in New Issue
Block a user