Skip to content

Commit

Permalink
fix: prevent overwriting hot repository on init
Browse files Browse the repository at this point in the history
This commit ensures that the hot repository won't get overwritten during
init when the non-hot repository does not exist.
  • Loading branch information
gilbsgilbs committed Nov 17, 2024
1 parent 093eab3 commit 5a8ebaf
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions crates/core/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,28 @@ impl<P, S> Repository<P, S> {
///
/// The id of the config file or `None` if no config file is found
pub fn config_id(&self) -> RusticResult<Option<ConfigId>> {
let config_ids = self.be.list(FileType::Config)?;
return self.config_id_with_backend(&self.be);
}

/// Returns the Id of the config file corresponding to a specific backend.
///
/// # Errors
///
/// * [`RepositoryErrorKind::ListingRepositoryConfigFileFailed`] - If listing the repository config file failed
/// * [`RepositoryErrorKind::MoreThanOneRepositoryConfig`] - If there is more than one repository config file
///
/// # Arguments
///
/// * `be` - The backend to use
///
/// # Returns
///
/// The id of the config file or `None` if no config file is found
///
/// [`RepositoryErrorKind::ListingRepositoryConfigFileFailed`]: crate::error::RepositoryErrorKind::ListingRepositoryConfigFileFailed
/// [`RepositoryErrorKind::MoreThanOneRepositoryConfig`]: crate::error::RepositoryErrorKind::MoreThanOneRepositoryConfig
fn config_id_with_backend(&self, be: &dyn WriteBackend) -> RusticResult<Option<ConfigId>> {
let config_ids = self.be.list(FileType::Config)?;
match config_ids.len() {
1 => Ok(Some(ConfigId::from(config_ids[0]))),
0 => Ok(None),
Expand Down Expand Up @@ -574,7 +594,12 @@ impl<P, S> Repository<P, S> {
key_opts: &KeyOptions,
config_opts: &ConfigOptions,
) -> RusticResult<Repository<P, OpenStatus>> {
if self.config_id()?.is_some() {
let config_exists = self.config_id_with_backend(&self.be)?.is_some();
let hot_config_exists = match self.be_hot {
None => false,
Some(ref be) => self.config_id_with_backend(be)?.is_some(),
};
if config_exists || hot_config_exists {
return Err(RusticError::new(
ErrorKind::Configuration,
"Config file already exists for `{name}`. Please check the repository.",
Expand Down

0 comments on commit 5a8ebaf

Please sign in to comment.