Skip to content

Commit

Permalink
Fix opening of too many files by keeping a list of path-strings in queue
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaslundberg committed Sep 7, 2019
1 parent 5b72ff5 commit 49cb849
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ fn scan(config: &Path, pattern: String) {
Ok(f) => f,
Err(e) => panic!("Could not open file {}", e),
};
let main_path = Path::new(pattern.as_str());

let dir: ReadDir = match fs::read_dir(main_path) {
Ok(dir) => dir,
Err(_) => panic!("Could not open directory {}", pattern),
};
let mut queue: VecDeque<String> = VecDeque::new();
queue.push_back(pattern);

let mut queue: VecDeque<ReadDir> = VecDeque::new();
queue.push_back(dir);
while let Some(path_str) = queue.pop_front() {
let path: &Path = Path::new(path_str.as_str());
let dir: ReadDir = match fs::read_dir(path) {
Ok(dir) => dir,
Err(_) => panic!("Failed to open dir {}", path_str),
};

while !queue.is_empty() {
let dir: ReadDir = queue.pop_front().unwrap();
for thing in dir {
let path: PathBuf = thing.unwrap().path();
if path.is_dir() {
Expand All @@ -59,8 +58,7 @@ fn scan(config: &Path, pattern: String) {
file.write(string).unwrap();
file.write(b"\n").unwrap();

let new_dir = fs::read_dir(path).unwrap();
queue.push_back(new_dir);
queue.push_back(String::from(path.as_path().to_str().unwrap()));
}
}
}
Expand Down

0 comments on commit 49cb849

Please sign in to comment.