@@ -77,6 +77,30 @@ impl File {
7777 |size|Ok ( sizeas u64 ) ,
7878)
7979}
80+
81+ /// Truncates or extends the underlying file, updating the size of this file to become `size`.
82+ ///
83+ /// If `size` is less than the current file's size, then the file will be shrunk. If it is greater
84+ /// than the currrent file's size, then the file will be extended to `size` and have all intermediate
85+ /// data filled with 0s.
86+ ///
87+ /// The file's cursor is not changed. In particular, if the cursor was at the end of the file and
88+ /// the file was shrunk using this operation, the cursor will now be past the end.
89+ ///
90+ /// If the requested length is greater than 9007199254740991 (max safe integer in a floating-point context),
91+ /// this will produce an error.
92+ pub async fn set_len ( & self , size : u64 ) -> io:: Result < ( ) > {
93+ const MAX_SAFE_INT : u64 = js_sys:: Number :: MAX_SAFE_INTEGER as _ ;
94+ if size >MAX_SAFE_INT {
95+ return Err ( std:: io:: Error :: new (
96+ std:: io:: ErrorKind :: InvalidInput ,
97+ format ! ( "requested size {size} too large, max allowed is {MAX_SAFE_INT}" ) ,
98+ ) ) ;
99+ }
100+ self . sync_access_handle
101+ . truncate_with_f64 ( sizeas _ )
102+ . map_err ( |err|OpfsError :: from ( err) . into_io_err ( ) )
103+ }
80104}
81105
82106impl File {