1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. */
15 
16 #include <libelf64/iter.h>
17 #include <libelf64/parse.h>
18 
19 #include <filesystem>
20 
21 namespace android {
22 namespace elf64 {
23 
ForEachElf64FromDir(const std::string & path,const Elf64Callback & callback)24 int ForEachElf64FromDir(const std::string& path, const Elf64Callback& callback) {
25     int nr_parsed = 0;
26 
27     for (const std::filesystem::directory_entry& dir_entry :
28         std::filesystem::recursive_directory_iterator(path)) {
29 
30         if (dir_entry.is_symlink() || !dir_entry.is_regular_file())
31             continue;
32 
33         std::string name = dir_entry.path();
34         android::elf64::Elf64Binary elf64Binary;
35         if (Elf64Parser::ParseElfFile(name, elf64Binary)) {
36             nr_parsed++;
37         } else {
38             continue;
39         }
40 
41         callback(elf64Binary);
42     }
43 
44     return nr_parsed;
45 }
46 
47 }  // namespace elf64
48 }  // namespace android
49 
50