scancode_rust/utils/
file.rs

1use chrono::{TimeZone, Utc};
2use glob::Pattern;
3use std::fs;
4use std::path::Path;
5
6/// Get the creation date of a file or directory as an RFC3339 string.
7pub fn get_creation_date(metadata: &fs::Metadata) -> Option<String> {
8    metadata.created().ok().map(|time: std::time::SystemTime| {
9        let seconds_since_epoch = time
10            .duration_since(std::time::UNIX_EPOCH)
11            .unwrap()
12            .as_secs() as i64;
13
14        Utc.timestamp_opt(seconds_since_epoch, 0)
15            .single()
16            .unwrap_or_else(|| Utc::now())
17            .to_rfc3339()
18    })
19}
20
21/// Check if a path should be excluded based on a list of glob patterns.
22pub fn is_path_excluded(path: &Path, exclude_patterns: &[Pattern]) -> bool {
23    let path_str = path.to_string_lossy();
24    let file_name = path
25        .file_name()
26        .map(|name| name.to_string_lossy())
27        .unwrap_or_default();
28
29    for pattern in exclude_patterns {
30        // Match against full path
31        if pattern.matches(&path_str) {
32            return true;
33        }
34
35        // Match against just the file/directory name
36        if pattern.matches(&file_name) {
37            return true;
38        }
39    }
40
41    false
42}