fn handle_glob_request()

in sources/logdog/src/log_request.rs [278:322]


fn handle_glob_request<P>(request: &LogRequest<'_>, tempdir: P) -> Result<()>
where
    P: AsRef<Path>,
{
    let mut files = HashSet::new();
    let glob_paths = glob(request.instructions).context(error::ParseGlobPattern {
        pattern: request.instructions,
    })?;
    for entry in glob_paths {
        if let Ok(path) = entry {
            if path.is_dir() {
                // iterate the directory and sub-directory to get all file paths
                for candidate in WalkDir::new(&path) {
                    if let Ok(e) = candidate {
                        if e.path().is_file() {
                            files.insert(e.into_path());
                        }
                    }
                }
            } else {
                files.insert(path);
            }
        }
    }
    for src_filepath in &files {
        // with glob pattern there are chances of multiple targets with same name, therefore
        // we maintain source file path and name in destination directory.
        // Eg. src file path "/a/b/file" will be converted to "dest_dir/a/b/file"
        let relative_path = src_filepath
            .strip_prefix("/")
            .unwrap_or(src_filepath.as_path());
        let dest_filepath = tempdir.as_ref().join(relative_path);
        let dest_dir_path = dest_filepath.parent().context(error::RootAsFile)?;
        // create directories in dest file path if it does not exist
        fs::create_dir_all(dest_dir_path).context(error::CreateOutputDirectory {
            path: dest_dir_path,
        })?;
        let _ = fs::copy(&src_filepath, &dest_filepath).with_context(|| error::FileCopy {
            request: request.to_string(),
            from: src_filepath.to_str().unwrap_or("<unknown>"),
            to: &dest_filepath,
        })?;
    }
    Ok(())
}