Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit eb8b135

Browse files
committed
add more defense code for File.finish(), #67
1 parent c30a303 commit eb8b135

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/file.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ impl Seek for VersionReader {
140140
///
141141
/// This is done by updating `File` using [`Write`] trait multiple times.
142142
/// 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.
144149
///
145150
/// ## Examples
146151
///
@@ -173,7 +178,8 @@ impl Seek for VersionReader {
173178
/// - **Single-part Write**
174179
///
175180
/// 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.
177183
///
178184
/// ## Examples
179185
///
@@ -579,20 +585,24 @@ impl Write for File {
579585
if self.wtr.is_none() {
580586
map_io_err!(self.begin_write())?;
581587
}
582-
match self.wtr {
588+
589+
let mut ret = 0;
590+
map_io_err!(match self.wtr {
583591
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(|| {
587594
ret = wtr.write(buf)?;
588595
Ok(())
589-
}))?;
590-
Ok(ret)
591-
}
596+
})
597+
.map(|_| ret),
592598
None => unreachable!(),
593599
},
594600
None => unreachable!(),
595601
}
602+
.or_else(|err| {
603+
self.wtr.take();
604+
Err(err)
605+
}))
596606
}
597607

598608
fn flush(&mut self) -> io::Result<()> {

0 commit comments

Comments
 (0)