@@ -5,7 +5,7 @@ use async_trait::async_trait;
55use x86_64:: structures:: paging:: { PageSize , Size4KiB } ;
66
77use crate :: {
8- error:: Result ,
8+ error:: { Result , ensure , err } ,
99 fs:: {
1010 FileSystem ,
1111 fd:: {
@@ -20,7 +20,8 @@ use crate::{
2020 user:: {
2121 futex:: Futexes ,
2222 syscall:: args:: {
23- FileMode , FileType , FileTypeAndMode , MemfdCreateFlags , OpenFlags , Seals , Stat , Timespec ,
23+ FileMode , FileType , FileTypeAndMode , MemfdCreateFlags , OpenFlags , Seals , Stat ,
24+ Timespec , Whence ,
2425 } ,
2526 thread:: { Gid , Uid } ,
2627 } ,
@@ -153,6 +154,43 @@ impl OpenFileDescription for MemFd {
153154 guard. buffer . truncate ( length)
154155 }
155156
157+ fn seek ( & self , offset : usize , whence : Whence , _: & mut FileAccessContext ) -> Result < usize > {
158+ let mut guard = self . internal . lock ( ) ;
159+
160+ match whence {
161+ Whence :: Set => guard. cursor_idx = offset,
162+ Whence :: Cur => {
163+ guard. cursor_idx = guard
164+ . cursor_idx
165+ . checked_add_signed ( offset as isize )
166+ . ok_or ( err ! ( Inval ) ) ?
167+ }
168+ Whence :: End => {
169+ let size = guard. buffer . len ( ) ;
170+ guard. cursor_idx = size
171+ . checked_add_signed ( offset as isize )
172+ . ok_or ( err ! ( Inval ) ) ?
173+ }
174+ Whence :: Data => {
175+ // Ensure that `offset` doesn't point past the file.
176+ ensure ! ( offset < guard. buffer. len( ) , NxIo ) ;
177+
178+ // We don't support holes so we always jump to `offset`.
179+ guard. cursor_idx = offset;
180+ }
181+ Whence :: Hole => {
182+ let size = guard. buffer . len ( ) ;
183+
184+ // Ensure that `offset` doesn't point past the file.
185+ ensure ! ( offset < size, NxIo ) ;
186+
187+ // We don't support holes so we always jump to the end of the file.
188+ guard. cursor_idx = size;
189+ }
190+ }
191+ Ok ( guard. cursor_idx )
192+ }
193+
156194 fn get_page ( & self , page_idx : usize , shared : bool ) -> Result < KernelPage > {
157195 let mut guard = self . internal . lock ( ) ;
158196 guard. buffer . get_page ( page_idx, shared)
0 commit comments