pxar: add error handling for MatchPattern::matches_filename()

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
This commit is contained in:
Christian Ebner 2019-08-05 14:28:03 +02:00 committed by Dietmar Maurer
parent 8c70e3eb18
commit 43e892d293
3 changed files with 16 additions and 12 deletions

View File

@ -687,7 +687,7 @@ impl <'a, W: Write> Encoder<'a, W> {
Err(err) => bail!("fstat {:?} failed - {}", self.full_path(), err),
};
match match_filename(&filename, &stat, &local_match_pattern) {
match match_filename(&filename, &stat, &local_match_pattern)? {
(MatchType::Positive, _) => {
let filename_osstr = std::ffi::OsStr::from_bytes(filename.to_bytes());
eprintln!("matched by .pxarexclude entry - skipping: {:?}", self.full_path().join(filename_osstr));
@ -1076,12 +1076,12 @@ fn match_filename(
filename: &CStr,
stat: &FileStat,
match_pattern: &Vec<MatchPattern>
) -> (MatchType, Vec<MatchPattern>) {
) -> Result<(MatchType, Vec<MatchPattern>), Error> {
let mut child_pattern = Vec::new();
let mut match_state = MatchType::None;
for pattern in match_pattern {
match pattern.matches_filename(filename, is_directory(&stat)) {
match pattern.matches_filename(filename, is_directory(&stat))? {
MatchType::None => {},
MatchType::Positive => match_state = MatchType::Positive,
MatchType::Negative => match_state = MatchType::Negative,
@ -1094,7 +1094,7 @@ fn match_filename(
}
}
(match_state, child_pattern)
Ok((match_state, child_pattern))
}
fn errno_is_unsupported(errno: Errno) -> bool {

View File

@ -143,7 +143,7 @@ impl MatchPattern {
}
}
pub fn matches_filename(&self, filename: &CStr, is_dir: bool) -> MatchType {
pub fn matches_filename(&self, filename: &CStr, is_dir: bool) -> Result<MatchType, Error> {
let mut res = MatchType::None;
let (front, _) = &self.split_pattern;
@ -152,7 +152,9 @@ impl MatchPattern {
let filename_ptr = filename.as_ptr() as *const libc::c_char;
fnmatch(front_ptr, filename_ptr , 0)
};
// TODO error cases
if fnmatch_res < 0 {
bail!("error in fnmatch inside of MatchPattern");
}
if fnmatch_res == 0 {
res = if self.match_positive {
MatchType::PartialPositive
@ -171,7 +173,9 @@ impl MatchPattern {
let filename_ptr = filename.as_ptr() as *const libc::c_char;
fnmatch(full_ptr, filename_ptr, 0)
};
// TODO error cases
if fnmatch_res < 0 {
bail!("error in fnmatch inside of MatchPattern");
}
if fnmatch_res == 0 {
res = if self.match_positive {
MatchType::Positive
@ -188,7 +192,7 @@ impl MatchPattern {
res = MatchType::None;
}
res
Ok(res)
}
}

View File

@ -750,7 +750,7 @@ impl <'a, R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<'a, R, F
// there are no match pattern.
let mut matched = parent_matched;
if match_pattern.len() > 0 {
match match_filename(filename, entry.mode as u32 & libc::S_IFMT == libc::S_IFDIR, match_pattern) {
match match_filename(filename, entry.mode as u32 & libc::S_IFMT == libc::S_IFDIR, match_pattern)? {
(MatchType::None, _) => matched = MatchType::None,
(MatchType::Negative, _) => matched = MatchType::Negative,
(match_type, pattern) => {
@ -1030,14 +1030,14 @@ fn match_filename(
filename: &OsStr,
is_dir: bool,
match_pattern: &Vec<MatchPattern>
) -> (MatchType, Vec<MatchPattern>) {
) -> Result<(MatchType, Vec<MatchPattern>), Error> {
let mut child_pattern = Vec::new();
let mut match_state = MatchType::None;
// read_filename() checks for nul bytes, so it is save to unwrap here
let name = CString::new(filename.as_bytes()).unwrap();
for pattern in match_pattern {
match pattern.matches_filename(&name, is_dir) {
match pattern.matches_filename(&name, is_dir)? {
MatchType::None => {},
MatchType::Positive => {
match_state = MatchType::Positive;
@ -1060,7 +1060,7 @@ fn match_filename(
}
}
(match_state, child_pattern)
Ok((match_state, child_pattern))
}
fn file_openat(parent: RawFd, filename: &OsStr, flags: OFlag, mode: Mode) -> Result<std::fs::File, Error> {