MCPcopy Create free account
hub / github.com/archlinux/alpm / collect_files

Function collect_files

alpm-common/src/package/input.rs:192–256  ·  view source on GitHub ↗

Collects all files in a `path` as a sorted list of paths and strips `init_path` from them. Recursively calls itself on all directories contained in `path`, retaining `init_path` and `filter` in these calls. When providing filenames using `filter`, paths that end in those filenames will be skipped and not returned in the list of paths. # Errors Returns an error if - calling [`read_dir`] on `pat

(
        path: &Path,
        init_path: &Path,
        filter: &[&str],
    )

Source from the content-addressed store, hash-verified

190 /// - an entry in one of the (sub)directories can not be retrieved,
191 /// - or stripping the prefix of a file in a (sub)directory fails.
192 fn collect_files(
193 path: &Path,
194 init_path: &Path,
195 filter: &[&str],
196 ) -> Result<Vec<PathBuf>, crate::Error> {
197 let mut paths = Vec::new();
198 let entries = read_dir(path).map_err(|source| crate::Error::IoPath {
199 path: path.to_path_buf(),
200 context: "reading entries of directory",
201 source,
202 })?;
203 for entry in entries {
204 let entry = entry.map_err(|source| crate::Error::IoPath {
205 path: path.to_path_buf(),
206 context: "reading entry in directory",
207 source,
208 })?;
209 let meta = entry.metadata().map_err(|source| crate::Error::IoPath {
210 path: entry.path(),
211 context: "getting metadata of file",
212 source,
213 })?;
214
215 // Ignore filtered files or directories.
216 if filter.iter().any(|filter| entry.path().ends_with(filter)) {
217 continue;
218 }
219
220 paths.push({
221 let mut path = entry
222 .path()
223 .strip_prefix(init_path)
224 .map_err(|source| crate::Error::PathStripPrefix {
225 prefix: path.to_path_buf(),
226 path: entry.path(),
227 source,
228 })?
229 .to_path_buf();
230
231 // Add a trailing "/" to directory paths, if there isn't one already.
232 if meta.is_dir()
233 && path
234 .as_os_str()
235 .to_str()
236 .is_some_and(|path| !path.ends_with(MAIN_SEPARATOR_STR))
237 {
238 path.as_mut_os_string().push(MAIN_SEPARATOR_STR);
239 }
240
241 path
242 });
243
244 // Call `collect_files` on each directory, retaining the initial `init_path` and
245 // `filter`.
246 if meta.is_dir() {
247 let mut subdir_paths = collect_files(entry.path().as_path(), init_path, filter)?;
248 paths.append(&mut subdir_paths);
249 }

Callers 1

relative_filesFunction · 0.85

Calls 6

metadataMethod · 0.80
is_dirMethod · 0.80
appendMethod · 0.80
to_path_bufMethod · 0.45
pathMethod · 0.45
as_pathMethod · 0.45

Tested by

no test coverage detected