File tree 2 files changed +23
-5
lines changed
cap-primitives/src/windows/fs
2 files changed +23
-5
lines changed Original file line number Diff line number Diff line change @@ -205,12 +205,14 @@ fn handle_open_result(
205
205
}
206
206
207
207
// Windows truncates symlinks into normal files, so truncation
208
- // may be disabled above; do it manually if needed.
208
+ // may be disabled above; do it manually if needed. Note that this
209
+ // is expected to always succeed for normal files, but this will
210
+ // fail if a directory was opened as directories don't support
211
+ // truncation.
209
212
if manually_trunc {
210
- // Unwrap is ok because 0 never overflows, and we'll only
211
- // have `manually_trunc` set when the file is opened for
212
- // writing.
213
- f. set_len ( 0 ) . unwrap ( ) ;
213
+ if let Err ( e) = f. set_len ( 0 ) {
214
+ return Err ( OpenUncheckedError :: Other ( e) ) ;
215
+ }
214
216
}
215
217
Ok ( f)
216
218
}
Original file line number Diff line number Diff line change @@ -1645,3 +1645,19 @@ fn create_dir_long_paths() {
1645
1645
std:: io:: ErrorKind :: NotFound
1646
1646
) ;
1647
1647
}
1648
+
1649
+ /// Ensure that opening a directory with the truncation flag set is an error,
1650
+ /// not a panic inside of cap-std.
1651
+ #[ test]
1652
+ fn open_directory_with_truncate_is_error ( ) {
1653
+ use cap_fs_ext:: OpenOptionsMaybeDirExt ;
1654
+ let tmpdir = tmpdir ( ) ;
1655
+ let mut options = OpenOptions :: new ( ) ;
1656
+ options
1657
+ . truncate ( true )
1658
+ . maybe_dir ( true )
1659
+ . read ( true )
1660
+ . write ( true ) ;
1661
+ tmpdir. create_dir ( "test" ) . unwrap ( ) ;
1662
+ assert ! ( tmpdir. open_with( "test" , & options) . is_err( ) ) ;
1663
+ }
You can’t perform that action at this time.
0 commit comments