file_util: drop open file instance from PersistError

PersistError is basically a pair of io::Error and NamedTempFile instance. It's
unlikely that we would want to propagate the open file instance to the CLI
error handler, leaving the temporary file alive.
This commit is contained in:
Yuya Nishihara 2023-12-16 17:20:48 +09:00
parent 35b8dad890
commit dd325c089c
3 changed files with 9 additions and 16 deletions

View file

@ -95,14 +95,14 @@ pub fn normalize_path(path: &Path) -> PathBuf {
pub fn persist_content_addressed_temp_file<P: AsRef<Path>>(
temp_file: NamedTempFile,
new_path: P,
) -> Result<File, PersistError> {
) -> io::Result<File> {
match temp_file.persist(&new_path) {
Ok(file) => Ok(file),
Err(PersistError { error, file }) => {
Err(PersistError { error, file: _ }) => {
if let Ok(existing_file) = File::open(new_path) {
Ok(existing_file)
} else {
Err(PersistError { error, file })
Err(error)
}
}
}

View file

@ -21,7 +21,7 @@ use std::io::{ErrorKind, Write};
use std::path::{Path, PathBuf};
use prost::Message;
use tempfile::{NamedTempFile, PersistError};
use tempfile::NamedTempFile;
use thiserror::Error;
use crate::backend::{CommitId, MillisSinceEpoch, ObjectId, Timestamp};
@ -34,12 +34,6 @@ use crate::op_store::{
};
use crate::{git, op_store};
impl From<PersistError> for OpStoreError {
fn from(err: PersistError) -> Self {
OpStoreError::Other(err.into())
}
}
#[derive(Debug, Error)]
#[error("Failed to read {kind} with ID {id}: {err}")]
struct DecodeError {
@ -119,7 +113,8 @@ impl OpStore for SimpleOpStore {
let id = ViewId::new(blake2b_hash(view).to_vec());
persist_content_addressed_temp_file(temp_file, self.view_path(&id))?;
persist_content_addressed_temp_file(temp_file, self.view_path(&id))
.map_err(|err| io_to_write_error(err, "view"))?;
Ok(id)
}
@ -148,7 +143,8 @@ impl OpStore for SimpleOpStore {
let id = OperationId::new(blake2b_hash(operation).to_vec());
persist_content_addressed_temp_file(temp_file, self.operation_path(&id))?;
persist_content_addressed_temp_file(temp_file, self.operation_path(&id))
.map_err(|err| io_to_write_error(err, "operation"))?;
Ok(id)
}
}

View file

@ -359,10 +359,7 @@ impl TableSegment for MutableTable {
#[derive(Debug, Error)]
#[error(transparent)]
pub enum TableStoreError {
IoError(#[from] io::Error),
PersistError(#[from] tempfile::PersistError),
}
pub struct TableStoreError(#[from] pub io::Error);
pub type TableStoreResult<T> = Result<T, TableStoreError>;