Skip to content
Merged
Changes from 2 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
45 changes: 28 additions & 17 deletions lib/vector-buffers/src/variants/disk_v2/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{io, path::Path};
use async_trait::async_trait;
use tokio::io::{AsyncRead, AsyncWrite};

const FILE_MODE_OWNER_RW_GROUP_RO: u32 = 0o640;

/// File metadata.
pub struct Metadata {
pub(crate) len: u64,
Expand Down Expand Up @@ -129,21 +131,21 @@ impl Filesystem for ProductionFilesystem {
type MutableMemoryMap = memmap2::MmapMut;

async fn open_file_writable(&self, path: &Path) -> io::Result<Self::File> {
tokio::fs::OpenOptions::new()
.append(true)
.read(true)
.create(true)
.open(path)
.await
let mut open_options = tokio::fs::OpenOptions::new();
open_options.append(true).read(true).create(true);

configure_file_open_options_for_write(&mut open_options);

open_options.open(path).await

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an undesirable pattern (passing &mut open_options) compared to the builder pattern of the original. Since the ordering of the mode option doesn't matter (I think? correct me if I'm wrong), could we wrap the new and mode bits into the custom function, resulting in something like:

Suggested change
let mut open_options = tokio::fs::OpenOptions::new();
open_options.append(true).read(true).create(true);
configure_file_open_options_for_write(&mut open_options);
open_options.open(path).await
new_file_with_write_options()
.append(true)
.read(true)
.create(true)
.open(path)
.await

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ba90f44, I've taken your suggestion and applied it to all areas that were creating some configuration of OpenOptions.

It's a little more code changed, but it deduplicates more code and the helper methods are at least documented so they drive home more of the intent, I believe.

}

async fn open_file_writable_atomic(&self, path: &Path) -> io::Result<Self::File> {
tokio::fs::OpenOptions::new()
.append(true)
.read(true)
.create_new(true)
.open(path)
.await
let mut open_options = tokio::fs::OpenOptions::new();
open_options.append(true).read(true).create_new(true);

configure_file_open_options_for_write(&mut open_options);

open_options.open(path).await
}

async fn open_file_readable(&self, path: &Path) -> io::Result<Self::File> {
Expand All @@ -157,11 +159,12 @@ impl Filesystem for ProductionFilesystem {
}

async fn open_mmap_writable(&self, path: &Path) -> io::Result<Self::MutableMemoryMap> {
let file = tokio::fs::OpenOptions::new()
.read(true)
.write(true)
.open(path)
.await?;
let mut open_options = tokio::fs::OpenOptions::new();
open_options.read(true).write(true);

configure_file_open_options_for_write(&mut open_options);

let file = open_options.open(path).await?;
let std_file = file.into_std().await;
unsafe { memmap2::MmapMut::map_mut(&std_file) }
}
Expand All @@ -171,6 +174,14 @@ impl Filesystem for ProductionFilesystem {
}
}

#[cfg(unix)]
fn configure_file_open_options_for_write(open_options: &mut tokio::fs::OpenOptions) {
open_options.mode(FILE_MODE_OWNER_RW_GROUP_RO);
}

#[cfg(not(unix))]
fn configure_file_open_options_for_write(_open_options: &mut tokio::fs::OpenOptions) {}

#[async_trait]
impl AsyncFile for tokio::fs::File {
async fn metadata(&self) -> io::Result<Metadata> {
Expand Down