@@ -140,7 +140,12 @@ impl Seek for VersionReader {
140
140
///
141
141
/// This is done by updating `File` using [`Write`] trait multiple times.
142
142
/// After all writing operations, [`finish`] must be called to create a new
143
- /// version.
143
+ /// version. Unless [`finish`] was successfully returned, no data will be
144
+ /// written to the file.
145
+ ///
146
+ /// Internally, a transaction is created when writing to the file and calling
147
+ /// [`finish`] will commit that transaction. Thus, you should not call
148
+ /// [`finish`] in case of any writing failure.
144
149
///
145
150
/// ## Examples
146
151
///
@@ -173,7 +178,8 @@ impl Seek for VersionReader {
173
178
/// - **Single-part Write**
174
179
///
175
180
/// This can be done by calling [`write_once`], which will call [`finish`]
176
- /// internally to create a new version.
181
+ /// internally to create a new version. Unless this method was successfully
182
+ /// returned, no data will be written to the file.
177
183
///
178
184
/// ## Examples
179
185
///
@@ -579,20 +585,24 @@ impl Write for File {
579
585
if self . wtr . is_none ( ) {
580
586
map_io_err ! ( self . begin_write( ) ) ?;
581
587
}
582
- match self . wtr {
588
+
589
+ let mut ret = 0 ;
590
+ map_io_err ! ( match self . wtr {
583
591
Some ( ref mut wtr) => match self . tx_handle {
584
- Some ( ref tx_handle) => {
585
- let mut ret = 0 ;
586
- map_io_err ! ( tx_handle. run( || {
592
+ Some ( ref tx_handle) => tx_handle
593
+ . run( || {
587
594
ret = wtr. write( buf) ?;
588
595
Ok ( ( ) )
589
- } ) ) ?;
590
- Ok ( ret)
591
- }
596
+ } )
597
+ . map( |_| ret) ,
592
598
None => unreachable!( ) ,
593
599
} ,
594
600
None => unreachable!( ) ,
595
601
}
602
+ . or_else( |err| {
603
+ self . wtr. take( ) ;
604
+ Err ( err)
605
+ } ) )
596
606
}
597
607
598
608
fn flush ( & mut self ) -> io:: Result < ( ) > {
0 commit comments