Directory: | cvmfs/ |
---|---|
File: | cvmfs/util/fs_traversal.h |
Date: | 2025-07-21 10:50:29 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 92 | 99 | 92.9% |
Branches: | 127 | 245 | 51.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | * | ||
4 | * It provides a file system traversal framework to abstract the traversal | ||
5 | * of directories. | ||
6 | */ | ||
7 | |||
8 | #ifndef CVMFS_UTIL_FS_TRAVERSAL_H_ | ||
9 | #define CVMFS_UTIL_FS_TRAVERSAL_H_ | ||
10 | |||
11 | #include <dirent.h> | ||
12 | #include <errno.h> | ||
13 | #include <sys/stat.h> | ||
14 | |||
15 | #include <cassert> | ||
16 | #include <cstdlib> | ||
17 | #include <string> | ||
18 | |||
19 | #include "util/exception.h" | ||
20 | #include "util/logging.h" | ||
21 | #include "util/platform.h" | ||
22 | |||
23 | #ifdef CVMFS_NAMESPACE_GUARD | ||
24 | namespace CVMFS_NAMESPACE_GUARD { | ||
25 | #endif | ||
26 | |||
27 | /** | ||
28 | * @brief A simple recursion engine to abstract the recursion of directories. | ||
29 | * It provides several callback hooks to instrument and control the recursion. | ||
30 | * Hooks will be called on the provided delegate object of type T | ||
31 | * | ||
32 | * Callbacks are called for every directory entry found by the recursion engine. | ||
33 | * The recursion can be influenced by return values of these callbacks. | ||
34 | */ | ||
35 | template<class T> | ||
36 | class FileSystemTraversal { | ||
37 | public: | ||
38 | typedef void (T::*VoidCallback)(const std::string &relative_path, | ||
39 | const std::string &dir_name); | ||
40 | typedef bool (T::*BoolCallback)(const std::string &relative_path, | ||
41 | const std::string &dir_name); | ||
42 | |||
43 | |||
44 | VoidCallback fn_enter_dir; | ||
45 | VoidCallback fn_leave_dir; | ||
46 | VoidCallback fn_new_file; | ||
47 | VoidCallback fn_new_symlink; | ||
48 | VoidCallback fn_new_socket; | ||
49 | VoidCallback fn_new_block_dev; | ||
50 | VoidCallback fn_new_character_dev; | ||
51 | VoidCallback fn_new_fifo; | ||
52 | |||
53 | /** | ||
54 | * Optional callback for all files during recursion to decide | ||
55 | * whether to completely ignore the file. If this callback returns | ||
56 | * true then the file will not be processed (this is a replacement | ||
57 | * for the ignored_files set, and it allows to ignore based on names | ||
58 | * or something else). If the function is not specified, no files | ||
59 | * will be ignored (except for "." and ".."). | ||
60 | */ | ||
61 | BoolCallback fn_ignore_file; | ||
62 | |||
63 | /** | ||
64 | * Callback if a directory was found. Depending on the response of | ||
65 | * the callback, the recursion will continue in the found directory/ | ||
66 | * If this callback is not specified, it will recurse by default. | ||
67 | */ | ||
68 | BoolCallback fn_new_dir_prefix; | ||
69 | |||
70 | /** | ||
71 | * Callback for a found directory after it was already recursed | ||
72 | * e.g. for deletion of directories: first delete content, | ||
73 | * then the directory itself | ||
74 | */ | ||
75 | VoidCallback fn_new_dir_postfix; | ||
76 | |||
77 | |||
78 | /** | ||
79 | * Create a new recursion engine | ||
80 | * @param delegate The object that will receive the callbacks | ||
81 | * @param relative_to_directory The DirEntries will be created relative | ||
82 | * to this directory | ||
83 | * @param recurse Should the traversal engine recurse? (if not, | ||
84 | * it just traverses the given directory) | ||
85 | */ | ||
86 | 23540 | FileSystemTraversal(T *delegate, | |
87 | const std::string &relative_to_directory, | ||
88 | const bool recurse) | ||
89 | 23540 | : fn_enter_dir(NULL) | |
90 | 23540 | , fn_leave_dir(NULL) | |
91 | 23540 | , fn_new_file(NULL) | |
92 | 23540 | , fn_new_symlink(NULL) | |
93 | 23540 | , fn_new_socket(NULL) | |
94 | 23540 | , fn_new_block_dev(NULL) | |
95 | 23540 | , fn_new_character_dev(NULL) | |
96 | 23540 | , fn_new_fifo(NULL) | |
97 | 23540 | , fn_ignore_file(NULL) | |
98 | 23540 | , fn_new_dir_prefix(NULL) | |
99 | 23540 | , fn_new_dir_postfix(NULL) | |
100 | 23540 | , delegate_(delegate) | |
101 | 23540 | , relative_to_directory_(relative_to_directory) | |
102 | 23540 | , recurse_(recurse) { | |
103 | 23540 | Init(); | |
104 | 23540 | } | |
105 | |||
106 | /** | ||
107 | * Start the recursion. | ||
108 | * @param dir_path The directory to start the recursion at | ||
109 | */ | ||
110 | 23540 | void Recurse(const std::string &dir_path) const { | |
111 |
13/18✓ Branch 0 taken 18827 times.
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 5607 times.
✓ Branch 3 taken 13220 times.
✓ Branch 4 taken 5607 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1124 times.
✓ Branch 7 taken 4483 times.
✓ Branch 8 taken 1124 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1101 times.
✓ Branch 11 taken 23 times.
✓ Branch 12 taken 1078 times.
✓ Branch 13 taken 23 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 1078 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
23540 | assert(fn_enter_dir != NULL || fn_leave_dir != NULL || fn_new_file != NULL |
112 | || fn_new_symlink != NULL || fn_new_dir_prefix != NULL | ||
113 | || fn_new_block_dev != NULL || fn_new_character_dev != NULL | ||
114 | || fn_new_fifo != NULL || fn_new_socket != NULL); | ||
115 | |||
116 |
6/12✓ Branch 1 taken 138 times.
✓ Branch 2 taken 18781 times.
✓ Branch 5 taken 138 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 138 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 138 times.
✓ Branch 12 taken 18781 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
23540 | assert(relative_to_directory_.length() == 0 |
117 | || dir_path.substr(0, relative_to_directory_.length()) | ||
118 | == relative_to_directory_); | ||
119 | |||
120 |
2/4✓ Branch 2 taken 18919 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 18919 times.
✗ Branch 6 not taken.
|
23540 | DoRecursion(dir_path, ""); |
121 | 23540 | } | |
122 | |||
123 | private: | ||
124 | // The delegate all hooks are called on | ||
125 | T *delegate_; | ||
126 | |||
127 | /** dir_path in callbacks will be relative to this directory */ | ||
128 | std::string relative_to_directory_; | ||
129 | bool recurse_; | ||
130 | |||
131 | |||
132 | 23540 | void Init() { } | |
133 | |||
134 | 47705 | void DoRecursion(const std::string &parent_path, | |
135 | const std::string &dir_name) const { | ||
136 | DIR *dip; | ||
137 | platform_dirent64 *dit; | ||
138 |
1/2✓ Branch 1 taken 40738 times.
✗ Branch 2 not taken.
|
47705 | const std::string path = parent_path |
139 |
6/12✓ Branch 1 taken 21819 times.
✓ Branch 2 taken 18919 times.
✓ Branch 4 taken 21819 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 18919 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 18919 times.
✓ Branch 11 taken 21819 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
95410 | + ((!dir_name.empty()) ? ("/" + dir_name) : ""); |
140 | |||
141 | // Change into directory and notify the user | ||
142 |
1/2✓ Branch 4 taken 40738 times.
✗ Branch 5 not taken.
|
47705 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "entering %s (%s -- %s)", |
143 | path.c_str(), parent_path.c_str(), dir_name.c_str()); | ||
144 |
1/2✓ Branch 2 taken 40738 times.
✗ Branch 3 not taken.
|
47705 | dip = opendir(path.c_str()); |
145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40738 times.
|
47705 | if (!dip) { |
146 | ✗ | PANIC(kLogStderr, | |
147 | "Failed to open %s (%d).\n" | ||
148 | "Please check directory permissions.", | ||
149 | path.c_str(), errno); | ||
150 | } | ||
151 |
1/2✓ Branch 1 taken 40738 times.
✗ Branch 2 not taken.
|
47705 | Notify(fn_enter_dir, parent_path, dir_name); |
152 | |||
153 | // Walk through the open directory notifying the user about contents | ||
154 |
3/4✓ Branch 1 taken 1884349 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1843611 times.
✓ Branch 4 taken 40738 times.
|
1976553 | while ((dit = platform_readdir(dip)) != NULL) { |
155 | // Check if file should be ignored | ||
156 |
14/32✓ Branch 2 taken 1843611 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1802873 times.
✓ Branch 6 taken 40738 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1802873 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 40738 times.
✓ Branch 13 taken 1762135 times.
✓ Branch 14 taken 1802873 times.
✓ Branch 15 taken 40738 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 1802873 times.
✓ Branch 18 taken 40738 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1843611 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 1843611 times.
✗ Branch 24 not taken.
✓ Branch 26 taken 81476 times.
✓ Branch 27 taken 1762135 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
|
1928848 | if (std::string(dit->d_name) == "." || std::string(dit->d_name) == "..") { |
157 | 95824 | continue; | |
158 |
2/2✓ Branch 0 taken 5865 times.
✓ Branch 1 taken 1756270 times.
|
1833438 | } else if (fn_ignore_file != NULL) { |
159 |
4/6✓ Branch 2 taken 5865 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 5865 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 207 times.
✓ Branch 10 taken 5658 times.
|
11730 | if (Notify(fn_ignore_file, path, dit->d_name)) { |
160 |
1/3✓ Branch 1 taken 207 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
414 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "ignoring %s/%s", |
161 | 414 | path.c_str(), dit->d_name); | |
162 | 414 | continue; | |
163 | } | ||
164 | } else { | ||
165 |
1/3✓ Branch 1 taken 1756270 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1821708 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, |
166 | "not ignoring %s/%s (fn_ignore_file not set)", path.c_str(), | ||
167 | 1821708 | dit->d_name); | |
168 | } | ||
169 | |||
170 | // Notify user about found directory entry | ||
171 | platform_stat64 info; | ||
172 |
2/4✓ Branch 1 taken 1761928 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1761928 times.
✗ Branch 5 not taken.
|
1833024 | const int retval = platform_lstat((path + "/" + dit->d_name).c_str(), |
173 | &info); | ||
174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1761928 times.
|
1833024 | if (retval != 0) { |
175 | ✗ | PANIC(kLogStderr, "failed to lstat '%s' errno: %d", | |
176 | (path + "/" + dit->d_name).c_str(), errno); | ||
177 | } | ||
178 |
2/2✓ Branch 0 taken 1629492 times.
✓ Branch 1 taken 132436 times.
|
1833024 | if (S_ISDIR(info.st_mode)) { |
179 |
1/3✓ Branch 1 taken 1629492 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1632505 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "passing directory %s/%s", |
180 | 1632505 | path.c_str(), dit->d_name); | |
181 |
10/20✓ Branch 2 taken 1629492 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1629492 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 301740 times.
✓ Branch 8 taken 1327752 times.
✓ Branch 9 taken 21819 times.
✓ Branch 10 taken 279921 times.
✓ Branch 11 taken 1629492 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 1629492 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 21819 times.
✓ Branch 18 taken 1607673 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1632505 | if (Notify(fn_new_dir_prefix, path, dit->d_name) && recurse_) { |
182 |
2/4✓ Branch 2 taken 21819 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 21819 times.
✗ Branch 6 not taken.
|
24165 | DoRecursion(path, dit->d_name); |
183 | } | ||
184 |
2/4✓ Branch 2 taken 1629492 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1629492 times.
✗ Branch 6 not taken.
|
1632505 | Notify(fn_new_dir_postfix, path, dit->d_name); |
185 |
2/2✓ Branch 0 taken 65093 times.
✓ Branch 1 taken 67343 times.
|
200519 | } else if (S_ISREG(info.st_mode)) { |
186 |
1/3✓ Branch 1 taken 65093 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
66243 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "passing regular file %s/%s", |
187 | 66243 | path.c_str(), dit->d_name); | |
188 |
2/4✓ Branch 2 taken 65093 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 65093 times.
✗ Branch 6 not taken.
|
66243 | Notify(fn_new_file, path, dit->d_name); |
189 |
2/2✓ Branch 0 taken 62792 times.
✓ Branch 1 taken 4551 times.
|
134276 | } else if (S_ISLNK(info.st_mode)) { |
190 |
1/3✓ Branch 1 taken 62792 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
125401 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "passing symlink %s/%s", |
191 | 125401 | path.c_str(), dit->d_name); | |
192 |
2/4✓ Branch 2 taken 62792 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 62792 times.
✗ Branch 6 not taken.
|
125401 | Notify(fn_new_symlink, path, dit->d_name); |
193 |
2/2✓ Branch 0 taken 664 times.
✓ Branch 1 taken 3887 times.
|
8875 | } else if (S_ISSOCK(info.st_mode)) { |
194 |
1/3✓ Branch 1 taken 664 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1101 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "passing socket %s/%s", |
195 | 1101 | path.c_str(), dit->d_name); | |
196 |
2/4✓ Branch 2 taken 664 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 664 times.
✗ Branch 6 not taken.
|
1101 | Notify(fn_new_socket, path, dit->d_name); |
197 |
2/2✓ Branch 0 taken 276 times.
✓ Branch 1 taken 3611 times.
|
7774 | } else if (S_ISBLK(info.st_mode)) { |
198 |
1/3✓ Branch 1 taken 276 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
552 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "passing block-device %s/%s", |
199 | 552 | path.c_str(), dit->d_name); | |
200 |
2/4✓ Branch 2 taken 276 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 276 times.
✗ Branch 6 not taken.
|
552 | Notify(fn_new_block_dev, path, dit->d_name); |
201 |
2/2✓ Branch 0 taken 3128 times.
✓ Branch 1 taken 483 times.
|
7222 | } else if (S_ISCHR(info.st_mode)) { |
202 |
1/3✓ Branch 1 taken 3128 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6256 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, |
203 | "passing character-device " | ||
204 | "%s/%s", | ||
205 | 6256 | path.c_str(), dit->d_name); | |
206 |
2/4✓ Branch 2 taken 3128 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3128 times.
✗ Branch 6 not taken.
|
6256 | Notify(fn_new_character_dev, path, dit->d_name); |
207 |
1/2✓ Branch 0 taken 483 times.
✗ Branch 1 not taken.
|
966 | } else if (S_ISFIFO(info.st_mode)) { |
208 |
1/3✓ Branch 1 taken 483 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
966 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "passing FIFO %s/%s", |
209 | 966 | path.c_str(), dit->d_name); | |
210 |
2/4✓ Branch 2 taken 483 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 483 times.
✗ Branch 6 not taken.
|
966 | Notify(fn_new_fifo, path, dit->d_name); |
211 | } else { | ||
212 | ✗ | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "unknown file type %s/%s", | |
213 | ✗ | path.c_str(), dit->d_name); | |
214 | } | ||
215 | } | ||
216 | |||
217 | // Close directory and notify user | ||
218 |
1/2✓ Branch 1 taken 40738 times.
✗ Branch 2 not taken.
|
47705 | closedir(dip); |
219 |
1/2✓ Branch 2 taken 40738 times.
✗ Branch 3 not taken.
|
47705 | LogCvmfs(kLogFsTraversal, kLogVerboseMsg, "leaving %s", path.c_str()); |
220 |
1/2✓ Branch 1 taken 40738 times.
✗ Branch 2 not taken.
|
47705 | Notify(fn_leave_dir, parent_path, dir_name); |
221 | 47705 | } | |
222 | |||
223 | 1644235 | inline bool Notify(const BoolCallback callback, | |
224 | const std::string &parent_path, | ||
225 | const std::string &entry_name) const { | ||
226 |
2/2✓ Branch 0 taken 1355505 times.
✓ Branch 1 taken 279852 times.
|
3008066 | return (callback == NULL) ? true |
227 |
6/11✓ Branch 0 taken 2461 times.
✓ Branch 1 taken 1353044 times.
✓ Branch 3 taken 1355505 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1355505 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 22095 times.
✓ Branch 9 taken 1333410 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
|
3008066 | : (delegate_->*callback)( |
228 |
2/2✓ Branch 0 taken 1355505 times.
✓ Branch 1 taken 279852 times.
|
3288470 | GetRelativePath(parent_path), entry_name); |
229 | } | ||
230 | |||
231 | 1928434 | inline void Notify(const VoidCallback callback, | |
232 | const std::string &parent_path, | ||
233 | const std::string &entry_name) const { | ||
234 |
2/2✓ Branch 0 taken 168234 times.
✓ Branch 1 taken 1675170 times.
|
1928434 | if (callback != NULL) { |
235 |
3/4✓ Branch 0 taken 9591 times.
✓ Branch 1 taken 158643 times.
✓ Branch 4 taken 168234 times.
✗ Branch 5 not taken.
|
241676 | (delegate_->*callback)(GetRelativePath(parent_path), entry_name); |
236 | } | ||
237 | 1928434 | } | |
238 | |||
239 | 1523739 | std::string GetRelativePath(const std::string &absolute_path) const { | |
240 | 1523739 | const unsigned int rel_dir_len = relative_to_directory_.length(); | |
241 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1441971 times.
|
1523739 | if (rel_dir_len >= absolute_path.length()) { |
242 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
7153 | return ""; |
243 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1441971 times.
|
1516586 | } else if (rel_dir_len > 1) { |
244 | 12466 | return absolute_path.substr(rel_dir_len + 1); | |
245 |
1/2✓ Branch 0 taken 1441971 times.
✗ Branch 1 not taken.
|
1504120 | } else if (rel_dir_len == 0) { |
246 | 1504120 | return absolute_path; | |
247 | ✗ | } else if (relative_to_directory_ == "/") { | |
248 | ✗ | return absolute_path.substr(1); | |
249 | } | ||
250 | |||
251 | ✗ | return ""; | |
252 | } | ||
253 | }; // FileSystemTraversal | ||
254 | |||
255 | #ifdef CVMFS_NAMESPACE_GUARD | ||
256 | } // namespace CVMFS_NAMESPACE_GUARD | ||
257 | #endif | ||
258 | |||
259 | #endif // CVMFS_UTIL_FS_TRAVERSAL_H_ | ||
260 |