@@ -2,7 +2,6 @@ use std::{
22 io:: { self , SeekFrom } ,
33 path:: Path ,
44 pin:: Pin ,
5- sync:: Mutex ,
65 task:: { Context , Poll } ,
76} ;
87
@@ -18,7 +17,7 @@ use super::{
1817#[ derive( Debug ) ]
1918pub struct File {
2019pub ( super ) sync_access_handle : FileSystemSyncAccessHandle ,
21- pub ( super ) pos : Mutex < u64 > ,
20+ pub ( super ) pos : Option < u64 > ,
2221}
2322
2423impl File {
@@ -81,27 +80,53 @@ impl File {
8180}
8281
8382impl File {
84- pub ( crate ) fn read_with_buf ( & self , buf : & mut [ u8 ] ) -> io:: Result < u64 > {
85- let options =FileSystemReadWriteOptions :: new ( ) ;
86- options. set_at ( * self . pos . lock ( ) . unwrap ( ) as f64 ) ;
87- let size =self
88- . sync_access_handle
89- . read_with_u8_array_and_options ( buf, & options)
90- . map_err ( |err|OpfsError :: from ( err) . into_io_err ( ) ) ?as u64 ;
91- Ok ( size)
92- }
93-
94- pub ( crate ) fn write_with_buf ( & self , buf : impl AsRef < [ u8 ] > ) -> io:: Result < u64 > {
95- let options =FileSystemReadWriteOptions :: new ( ) ;
96- options. set_at ( * self . pos . lock ( ) . unwrap ( ) as f64 ) ;
97- let size =self
98- . sync_access_handle
99- . write_with_u8_array_and_options ( buf. as_ref ( ) , & options)
100- . map_err ( |err|OpfsError :: from ( err) . into_io_err ( ) ) ?as u64 ;
101- Ok ( size)
102- }
103-
104- pub ( crate ) fn flush ( & self ) -> io:: Result < ( ) > {
83+ pub ( crate ) fn read_to_buf ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < u64 > {
84+ match self . pos {
85+ Some ( pos) =>{
86+ let options =FileSystemReadWriteOptions :: new ( ) ;
87+ options. set_at ( posas f64 ) ;
88+ let size =self
89+ . sync_access_handle
90+ . read_with_u8_array_and_options ( buf, & options)
91+ . map_err ( |err|OpfsError :: from ( err) . into_io_err ( ) ) ?
92+ as u64 ;
93+ Ok ( size)
94+ }
95+ None =>{
96+ let size =self
97+ . sync_access_handle
98+ . read_with_u8_array ( buf)
99+ . map_err ( |err|OpfsError :: from ( err) . into_io_err ( ) ) ?
100+ as u64 ;
101+ Ok ( size)
102+ }
103+ }
104+ }
105+
106+ pub ( crate ) fn write_with_buf ( & mut self , buf : impl AsRef < [ u8 ] > ) -> io:: Result < u64 > {
107+ match self . pos {
108+ Some ( pos) =>{
109+ let options =FileSystemReadWriteOptions :: new ( ) ;
110+ options. set_at ( posas f64 ) ;
111+ let size =self
112+ . sync_access_handle
113+ . write_with_u8_array_and_options ( buf. as_ref ( ) , & options)
114+ . map_err ( |err|OpfsError :: from ( err) . into_io_err ( ) ) ?
115+ as u64 ;
116+ Ok ( size)
117+ }
118+ None =>{
119+ let size =self
120+ . sync_access_handle
121+ . write_with_u8_array ( buf. as_ref ( ) )
122+ . map_err ( |err|OpfsError :: from ( err) . into_io_err ( ) ) ?
123+ as u64 ;
124+ Ok ( size)
125+ }
126+ }
127+ }
128+
129+ pub ( super ) fn flush ( & self ) -> io:: Result < ( ) > {
105130self . sync_access_handle
106131. flush ( )
107132. map_err ( |err|OpfsError :: from ( err) . into_io_err ( ) )
@@ -120,29 +145,27 @@ impl Drop for File {
120145
121146impl AsyncRead for File {
122147fn poll_read (
123- self : Pin < & mut Self > ,
148+ mut self : Pin < & mut Self > ,
124149_cx : & mut Context < ' _ > ,
125150buf : & mut [ u8 ] ,
126151) ->Poll < io:: Result < usize > > {
127- let size =self . read_with_buf ( buf) ?;
152+ let offset =self . read_to_buf ( buf) ?;
128153
129- let mut pos =self . pos . lock ( ) . unwrap ( ) ;
130- * pos += size;
154+ self . pos =Some ( self . pos . unwrap_or_default ( ) + offset) ;
131155
132- Poll :: Ready ( Ok ( size as usize ) )
156+ Poll :: Ready ( Ok ( offset as usize ) )
133157}
134158}
135159
136160impl AsyncWrite for File {
137161fn poll_write (
138- self : Pin < & mut Self > ,
162+ mut self : Pin < & mut Self > ,
139163_cx : & mut Context < ' _ > ,
140164buf : & [ u8 ] ,
141165) ->Poll < Result < usize , io:: Error > > {
142166let offset =self . write_with_buf ( buf) ?;
143167
144- let mut pos =self . pos . lock ( ) . unwrap ( ) ;
145- * pos += offset;
168+ self . pos =Some ( self . pos . unwrap_or_default ( ) + offset) ;
146169
147170Poll :: Ready ( Ok ( offsetas usize ) )
148171}
@@ -160,27 +183,30 @@ impl AsyncWrite for File {
160183
161184impl AsyncSeek for File {
162185fn poll_seek (
163- self : Pin < & mut Self > ,
186+ mut self : Pin < & mut Self > ,
164187_cx : & mut Context < ' _ > ,
165188position : SeekFrom ,
166189) ->Poll < io:: Result < u64 > > {
167- let mut pos =self . pos . lock ( ) . unwrap ( ) ;
168190match position{
169191SeekFrom :: Start ( offset) =>{
170- * pos = offset;
192+ self . pos =Some ( offset) ;
171193}
172194SeekFrom :: End ( offset) =>{
173- * pos =self
174- . size ( ) ?
175- . checked_add_signed ( offset)
176- . ok_or ( io:: Error :: from ( io:: ErrorKind :: InvalidInput ) ) ?;
195+ self . pos =Some (
196+ self . size ( ) ?
197+ . checked_add_signed ( offset)
198+ . ok_or ( io:: Error :: from ( io:: ErrorKind :: InvalidInput ) ) ?,
199+ ) ;
177200}
178201SeekFrom :: Current ( offset) =>{
179- * pos = pos
180- . checked_add_signed ( offset)
181- . ok_or ( io:: Error :: from ( io:: ErrorKind :: InvalidInput ) ) ?;
202+ self . pos =Some (
203+ self . pos
204+ . unwrap_or_default ( )
205+ . checked_add_signed ( offset)
206+ . ok_or ( io:: Error :: from ( io:: ErrorKind :: InvalidInput ) ) ?,
207+ ) ;
182208}
183209}
184- Poll :: Ready ( Ok ( * pos) )
210+ Poll :: Ready ( Ok ( self . pos . unwrap_or_default ( ) ) )
185211}
186212}