Mercurial > public > mercurial-scm > hg-stable
diff rust/hg-core/src/repo.rs @ 46638:1f55cd5b292f
rust: Add a log file rotation utility
This is ported to Rust from `mercurial/loggingutil.py`.
The "builder" pattern is used to make it visible at call sites what the two
numeric parameters mean. In Python they might simply by keyword arguments.
Differential Revision: https://phab.mercurial-scm.org/D10010
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Thu, 11 Feb 2021 15:51:11 +0100 |
parents | 5ce2aa7c2ad5 |
children | 755c31a1caf9 |
line wrap: on
line diff
--- a/rust/hg-core/src/repo.rs Tue Feb 16 15:22:20 2021 +0100 +++ b/rust/hg-core/src/repo.rs Thu Feb 11 15:51:11 2021 +0100 @@ -1,5 +1,5 @@ use crate::config::{Config, ConfigError, ConfigParseError}; -use crate::errors::{HgError, IoResultExt}; +use crate::errors::{HgError, IoErrorContext, IoResultExt}; use crate::requirements; use crate::utils::current_dir; use crate::utils::files::get_path_from_bytes; @@ -38,8 +38,8 @@ /// Filesystem access abstraction for the contents of a given "base" diretory #[derive(Clone, Copy)] -pub(crate) struct Vfs<'a> { - base: &'a Path, +pub struct Vfs<'a> { + pub(crate) base: &'a Path, } impl Repo { @@ -196,12 +196,12 @@ /// For accessing repository files (in `.hg`), except for the store /// (`.hg/store`). - pub(crate) fn hg_vfs(&self) -> Vfs<'_> { + pub fn hg_vfs(&self) -> Vfs<'_> { Vfs { base: &self.dot_hg } } /// For accessing repository store files (in `.hg/store`) - pub(crate) fn store_vfs(&self) -> Vfs<'_> { + pub fn store_vfs(&self) -> Vfs<'_> { Vfs { base: &self.store } } @@ -209,7 +209,7 @@ // The undescore prefix silences the "never used" warning. Remove before // using. - pub(crate) fn _working_directory_vfs(&self) -> Vfs<'_> { + pub fn _working_directory_vfs(&self) -> Vfs<'_> { Vfs { base: &self.working_directory, } @@ -217,26 +217,38 @@ } impl Vfs<'_> { - pub(crate) fn join(&self, relative_path: impl AsRef<Path>) -> PathBuf { + pub fn join(&self, relative_path: impl AsRef<Path>) -> PathBuf { self.base.join(relative_path) } - pub(crate) fn read( + pub fn read( &self, relative_path: impl AsRef<Path>, ) -> Result<Vec<u8>, HgError> { let path = self.join(relative_path); - std::fs::read(&path).for_file(&path) + std::fs::read(&path).when_reading_file(&path) } - pub(crate) fn mmap_open( + pub fn mmap_open( &self, relative_path: impl AsRef<Path>, ) -> Result<Mmap, HgError> { let path = self.base.join(relative_path); - let file = std::fs::File::open(&path).for_file(&path)?; + let file = std::fs::File::open(&path).when_reading_file(&path)?; // TODO: what are the safety requirements here? - let mmap = unsafe { MmapOptions::new().map(&file) }.for_file(&path)?; + let mmap = unsafe { MmapOptions::new().map(&file) } + .when_reading_file(&path)?; Ok(mmap) } + + pub fn rename( + &self, + relative_from: impl AsRef<Path>, + relative_to: impl AsRef<Path>, + ) -> Result<(), HgError> { + let from = self.join(relative_from); + let to = self.join(relative_to); + std::fs::rename(&from, &to) + .with_context(|| IoErrorContext::RenamingFile { from, to }) + } }