Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a with_truncate_on_close to DmaStreamWriterBuilder #612

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion glommio/src/io/dma_file_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ pub struct DmaStreamWriterBuilder {
buffer_size: usize,
write_behind: usize,
sync_on_close: bool,
truncate_on_close: bool,
file: Rc<DmaFile>,
}

Expand Down Expand Up @@ -654,6 +655,7 @@ impl DmaStreamWriterBuilder {
buffer_size: 128 << 10,
write_behind: 4,
sync_on_close: true,
truncate_on_close: false,
file: Rc::new(file),
}
}
Expand All @@ -680,6 +682,15 @@ impl DmaStreamWriterBuilder {
self
}

/// Issue a truncate operation to the position at the end of the last write
/// when closing the file, thus removing any padding that might have been
/// added.
#[must_use = "The builder must be built to be useful"]
pub fn with_truncate_on_close(mut self, truncate_enabled: bool) -> Self {
self.truncate_on_close = truncate_enabled;
self
}

/// Define the buffer size that will be used by the [`DmaStreamWriter`]
///
/// [`DmaStreamWriter`]: struct.DmaStreamWriter.html
Expand Down Expand Up @@ -800,6 +811,7 @@ struct DmaStreamWriterState {
buffer_pos: usize,
write_behind: usize,
sync_on_close: bool,
truncate_on_close: bool,
}

macro_rules! already_closed {
Expand Down Expand Up @@ -884,7 +896,7 @@ impl DmaStreamWriterState {

let must_truncate = {
let state = state.borrow();
state.must_truncate()
state.truncate_on_close || state.must_truncate()
};

if must_truncate {
Expand Down Expand Up @@ -1024,6 +1036,7 @@ impl DmaStreamWriter {
buffer_size: builder.buffer_size,
write_behind: builder.write_behind,
sync_on_close: builder.sync_on_close,
truncate_on_close: builder.truncate_on_close,
current_buffer: None,
waker: None,
flush_state: DmaStreamWriterFlushState::new(builder.write_behind),
Expand Down
Loading