fix: rename the written out file in Layer ctor

This commit is contained in:
Joonas Koivunen
2023-08-24 22:21:49 +03:00
parent ef1c3d3914
commit 1559ef36af
3 changed files with 9 additions and 15 deletions

View File

@@ -561,12 +561,7 @@ impl DeltaLayerWriterInner {
// fsync the file
file.sync_all()?;
let layer = Layer::for_written(self.conf, timeline, desc)?;
// Rename the file to its final name
//
// Note: This overwrites any existing file. There shouldn't be any.
// FIXME: throw an error instead?
std::fs::rename(self.path, layer.local_path())?;
let layer = Layer::for_written_tempfile(self.conf, timeline, desc, &self.path)?;
trace!("created delta layer {}", layer.local_path().display());

View File

@@ -495,13 +495,7 @@ impl ImageLayerWriterInner {
// fsync the file
file.sync_all()?;
let layer = Layer::for_written(self.conf, timeline, desc)?;
// Rename the file to its final name
//
// Note: This overwrites any existing file. There shouldn't be any.
// FIXME: throw an error instead?
std::fs::rename(self.path, layer.local_path())?;
let layer = Layer::for_written_tempfile(self.conf, timeline, desc, &self.path)?;
trace!("created image layer {}", layer.local_path().display());

View File

@@ -120,10 +120,13 @@ impl Layer {
ResidentLayer { downloaded, owner }
}
pub(crate) fn for_written(
/// Creates a Layer value for freshly written out new layer file by renaming it from a
/// temporary path.
pub(crate) fn for_written_tempfile(
conf: &'static PageServerConf,
timeline: &Arc<Timeline>,
desc: PersistentLayerDesc,
temp_path: &Path,
) -> anyhow::Result<ResidentLayer> {
let mut resident = None;
@@ -143,7 +146,9 @@ impl Layer {
let downloaded = resident.expect("just initialized");
// FIXME: should we handle the rename?
// if the rename works, the path is as expected
std::fs::rename(temp_path, owner.local_path())
.context("rename temporary file as correct path for {owner}")?;
Ok(ResidentLayer { downloaded, owner })
}