which/
checker.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::finder::Checker;
use crate::{NonFatalError, NonFatalErrorHandler};
use std::fs;
use std::path::Path;

pub struct ExecutableChecker;

impl ExecutableChecker {
    pub fn new() -> ExecutableChecker {
        ExecutableChecker
    }
}

impl Checker for ExecutableChecker {
    #[cfg(any(unix, target_os = "wasi", target_os = "redox"))]
    fn is_valid<F: NonFatalErrorHandler>(
        &self,
        path: &Path,
        nonfatal_error_handler: &mut F,
    ) -> bool {
        use std::io;

        use rustix::fs as rfs;
        let ret = rfs::access(path, rfs::Access::EXEC_OK)
            .map_err(|e| {
                nonfatal_error_handler.handle(NonFatalError::Io(io::Error::from_raw_os_error(
                    e.raw_os_error(),
                )))
            })
            .is_ok();
        #[cfg(feature = "tracing")]
        tracing::trace!("{} EXEC_OK = {ret}", path.display());
        ret
    }

    #[cfg(windows)]
    fn is_valid<F: NonFatalErrorHandler>(
        &self,
        _path: &Path,
        _nonfatal_error_handler: &mut F,
    ) -> bool {
        true
    }
}

pub struct ExistedChecker;

impl ExistedChecker {
    pub fn new() -> ExistedChecker {
        ExistedChecker
    }
}

impl Checker for ExistedChecker {
    #[cfg(target_os = "windows")]
    fn is_valid<F: NonFatalErrorHandler>(
        &self,
        path: &Path,
        nonfatal_error_handler: &mut F,
    ) -> bool {
        let ret = fs::symlink_metadata(path)
            .map(|metadata| {
                let file_type = metadata.file_type();
                #[cfg(feature = "tracing")]
                tracing::trace!(
                    "{} is_file() = {}, is_symlink() = {}",
                    path.display(),
                    file_type.is_file(),
                    file_type.is_symlink()
                );
                file_type.is_file() || file_type.is_symlink()
            })
            .map_err(|e| {
                nonfatal_error_handler.handle(NonFatalError::Io(e));
            })
            .unwrap_or(false)
            && (path.extension().is_some() || matches_arch(path, nonfatal_error_handler));
        #[cfg(feature = "tracing")]
        tracing::trace!(
            "{} has_extension = {}, ExistedChecker::is_valid() = {ret}",
            path.display(),
            path.extension().is_some()
        );
        ret
    }

    #[cfg(not(target_os = "windows"))]
    fn is_valid<F: NonFatalErrorHandler>(
        &self,
        path: &Path,
        nonfatal_error_handler: &mut F,
    ) -> bool {
        let ret = fs::metadata(path).map(|metadata| metadata.is_file());
        #[cfg(feature = "tracing")]
        tracing::trace!("{} is_file() = {ret:?}", path.display());
        match ret {
            Ok(ret) => ret,
            Err(e) => {
                nonfatal_error_handler.handle(NonFatalError::Io(e));
                false
            }
        }
    }
}

#[cfg(target_os = "windows")]
fn matches_arch<F: NonFatalErrorHandler>(path: &Path, nonfatal_error_handler: &mut F) -> bool {
    use std::io;

    let ret = winsafe::GetBinaryType(&path.display().to_string())
        .map_err(|e| {
            nonfatal_error_handler.handle(NonFatalError::Io(io::Error::from_raw_os_error(
                e.raw() as i32
            )))
        })
        .is_ok();
    #[cfg(feature = "tracing")]
    tracing::trace!("{} matches_arch() = {ret}", path.display());
    ret
}

pub struct CompositeChecker {
    existed_checker: ExistedChecker,
    executable_checker: ExecutableChecker,
}

impl CompositeChecker {
    pub fn new() -> CompositeChecker {
        CompositeChecker {
            executable_checker: ExecutableChecker::new(),
            existed_checker: ExistedChecker::new(),
        }
    }
}

impl Checker for CompositeChecker {
    fn is_valid<F: NonFatalErrorHandler>(
        &self,
        path: &Path,
        nonfatal_error_handler: &mut F,
    ) -> bool {
        self.existed_checker.is_valid(path, nonfatal_error_handler)
            && self
                .executable_checker
                .is_valid(path, nonfatal_error_handler)
    }
}