comparison rust/hg-core/src/utils/files.rs @ 46187:95d6f31e88db

hg-core: add basic config module The config module exposes a `Config` struct, unused for now. It only reads the config file local to the repository, but handles all valid patterns and includes/unsets. It is structured in layers instead of erasing by reverse order of precedence, allowing us to transparently know more about the config for debugging purposes, and potentially other things I haven't thought about yet. This change also introduces `format_bytes!` to `hg-core`. Differential Revision: https://phab.mercurial-scm.org/D9408
author Raphaël Gomès <rgomes@octobus.net>
date Tue, 29 Dec 2020 10:53:45 +0100
parents 1b3197047f5c
children 0d734c0ae1cf
comparison
equal deleted inserted replaced
46186:5f27924a201d 46187:95d6f31e88db
16 }; 16 };
17 use lazy_static::lazy_static; 17 use lazy_static::lazy_static;
18 use same_file::is_same_file; 18 use same_file::is_same_file;
19 use std::borrow::{Cow, ToOwned}; 19 use std::borrow::{Cow, ToOwned};
20 use std::fs::Metadata; 20 use std::fs::Metadata;
21 use std::io::Read;
21 use std::iter::FusedIterator; 22 use std::iter::FusedIterator;
22 use std::ops::Deref; 23 use std::ops::Deref;
23 use std::path::{Path, PathBuf}; 24 use std::path::{Path, PathBuf};
24 25
25 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path { 26 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path {
306 } 307 }
307 Cow::Owned(res) 308 Cow::Owned(res)
308 } 309 }
309 } 310 }
310 311
312 /// Reads a file in one big chunk instead of doing multiple reads
313 pub fn read_whole_file(filepath: &Path) -> std::io::Result<Vec<u8>> {
314 let mut file = std::fs::File::open(filepath)?;
315 let size = file.metadata()?.len();
316
317 let mut res = vec![0; size as usize];
318 file.read_exact(&mut res)?;
319
320 Ok(res)
321 }
322
311 #[cfg(test)] 323 #[cfg(test)]
312 mod tests { 324 mod tests {
313 use super::*; 325 use super::*;
314 use pretty_assertions::assert_eq; 326 use pretty_assertions::assert_eq;
315 327