mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-24 06:27:39 +00:00
runtime-rs: fix the bug of func count_files
When the total number of files observed is greater than limit, return -1 directly. runtime has fixed this bug, it should b ported to runtime-rs. Fixes:#9829 Signed-off-by: gaohuatao <gaohuatao@bytedance.com>
This commit is contained in:
parent
3a0247ed43
commit
4cb4e44234
@ -130,7 +130,11 @@ fn count_files<P: AsRef<Path>>(path: P, limit: i32) -> std::io::Result<i32> {
|
|||||||
let file = entry?;
|
let file = entry?;
|
||||||
let p = file.path();
|
let p = file.path();
|
||||||
if p.is_dir() {
|
if p.is_dir() {
|
||||||
num_files += count_files(&p, limit)?;
|
let inc = count_files(&p, limit - num_files)?;
|
||||||
|
if inc == -1 {
|
||||||
|
return Ok(-1);
|
||||||
|
}
|
||||||
|
num_files += inc;
|
||||||
} else {
|
} else {
|
||||||
num_files += 1;
|
num_files += 1;
|
||||||
}
|
}
|
||||||
@ -165,6 +169,40 @@ mod tests {
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use test_utils::skip_if_not_root;
|
use test_utils::skip_if_not_root;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_count_files() {
|
||||||
|
let limit = 8;
|
||||||
|
let test_tmp_dir = tempfile::tempdir().expect("failed to create tempdir");
|
||||||
|
let work_path = test_tmp_dir.path().join("work");
|
||||||
|
|
||||||
|
let result = fs::create_dir_all(&work_path);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
|
||||||
|
let origin_dir = work_path.join("origin_dir");
|
||||||
|
let result = fs::create_dir_all(&origin_dir);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
for n in 0..limit {
|
||||||
|
let tmp_file = origin_dir.join(format!("file{}", n));
|
||||||
|
let res = fs::File::create(tmp_file);
|
||||||
|
assert!(res.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
let symlink_origin_dir = work_path.join("symlink_origin_dir");
|
||||||
|
let result = std::os::unix::fs::symlink(&origin_dir, &symlink_origin_dir);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
for n in 0..2 {
|
||||||
|
let tmp_file = work_path.join(format!("file{}", n));
|
||||||
|
let res = fs::File::create(tmp_file);
|
||||||
|
assert!(res.is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
let count = count_files(&work_path, limit).unwrap_or(0);
|
||||||
|
assert_eq!(count, -1);
|
||||||
|
|
||||||
|
let count = count_files(&origin_dir, limit).unwrap_or(0);
|
||||||
|
assert_eq!(count, limit);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_watchable_mount() {
|
fn test_is_watchable_mount() {
|
||||||
skip_if_not_root!();
|
skip_if_not_root!();
|
||||||
|
Loading…
Reference in New Issue
Block a user