• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2025 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 
17 #pragma once
18 
19 #include <android-base/result.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 
23 #include <functional>
24 #include <string>
25 #include <vector>
26 
27 struct Entry {
28   // file mode
29   mode_t mode;
30 
31   // path to this entry.
32   // - each entry should start with './'
33   // - directory entry should end with '/'
34   std::string path;
35 
36   std::string security_context;
37 };
38 
39 // Generic lister
40 template <typename ReadEntry, typename ReadDir>
List(ReadEntry read_entry,ReadDir read_dir)41 android::base::Result<std::vector<Entry>> List(ReadEntry read_entry,
42                                                ReadDir read_dir) {
43   namespace fs = std::filesystem;
44   using namespace android::base;
45 
46   std::vector<Entry> entries;
47 
48   // Recursive visitor
49   std::function<Result<void>(const fs::path& path)> visit =
50       [&](const fs::path& path) -> Result<void> {
51     auto entry = OR_RETURN(read_entry(path));
52     entries.push_back(entry);
53 
54     if (S_ISDIR(entry.mode)) {
55       auto names = OR_RETURN(read_dir(path));
56       std::ranges::sort(names);
57       for (auto name : names) {
58         // Skip . and ..
59         if (name == "." || name == "..") continue;
60         OR_RETURN(visit(path / name));
61       }
62     }
63     return {};
64   };
65 
66   // Visit each path entry recursively starting from root
67   OR_RETURN(visit("."));
68 
69   return entries;
70 }