comparison rust/hg-core/src/utils/files.rs @ 44265:c18dd48cea4a

rust-pathauditor: add Rust implementation of the `pathauditor` It does not offer the same flexibility as the Python implementation, but should check incoming paths just as well. Differential Revision: https://phab.mercurial-scm.org/D7866
author Raphaël Gomès <rgomes@octobus.net>
date Wed, 05 Feb 2020 17:05:37 +0100
parents cf065c6a0197
children 0e9ac3968b56
comparison
equal deleted inserted replaced
44264:d3f776c4760e 44265:c18dd48cea4a
10 //! Functions for fiddling with files. 10 //! Functions for fiddling with files.
11 11
12 use crate::utils::hg_path::{HgPath, HgPathBuf}; 12 use crate::utils::hg_path::{HgPath, HgPathBuf};
13 use std::iter::FusedIterator; 13 use std::iter::FusedIterator;
14 14
15 use crate::utils::replace_slice;
16 use lazy_static::lazy_static;
15 use std::fs::Metadata; 17 use std::fs::Metadata;
16 use std::path::Path; 18 use std::path::Path;
17 19
18 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path { 20 pub fn get_path_from_bytes(bytes: &[u8]) -> &Path {
19 let os_str; 21 let os_str;
83 return path.to_ascii_uppercase(); 85 return path.to_ascii_uppercase();
84 #[cfg(unix)] 86 #[cfg(unix)]
85 path.to_ascii_lowercase() 87 path.to_ascii_lowercase()
86 } 88 }
87 89
90 lazy_static! {
91 static ref IGNORED_CHARS: Vec<Vec<u8>> = {
92 [
93 0x200c, 0x200d, 0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d,
94 0x202e, 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
95 ]
96 .iter()
97 .map(|code| {
98 std::char::from_u32(*code)
99 .unwrap()
100 .encode_utf8(&mut [0; 3])
101 .bytes()
102 .collect()
103 })
104 .collect()
105 };
106 }
107
108 fn hfs_ignore_clean(bytes: &[u8]) -> Vec<u8> {
109 let mut buf = bytes.to_owned();
110 let needs_escaping = bytes.iter().any(|b| *b == b'\xe2' || *b == b'\xef');
111 if needs_escaping {
112 for forbidden in IGNORED_CHARS.iter() {
113 replace_slice(&mut buf, forbidden, &[])
114 }
115 buf
116 } else {
117 buf
118 }
119 }
120
121 pub fn lower_clean(bytes: &[u8]) -> Vec<u8> {
122 hfs_ignore_clean(&bytes.to_ascii_lowercase())
123 }
124
88 #[derive(Eq, PartialEq, Ord, PartialOrd, Copy, Clone)] 125 #[derive(Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
89 pub struct HgMetadata { 126 pub struct HgMetadata {
90 pub st_dev: u64, 127 pub st_dev: u64,
91 pub st_mode: u32, 128 pub st_mode: u32,
92 pub st_nlink: u64, 129 pub st_nlink: u64,