Skip to content

Commit d6d252e

Browse files
committed
fix conditional build
1 parent 7972bea commit d6d252e

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

dsc_lib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ tree-sitter-rust = "0.24"
4949
tree-sitter-dscexpression = { path = "../tree-sitter-dscexpression" }
5050
uuid = { version = "1.18", features = ["v4"] }
5151
which = "8.0"
52+
53+
[target.'cfg(windows)'.dependencies]
5254
windows = { version = "0.62", features = [
5355
"Win32_Foundation",
5456
"Win32_Security",

dsc_lib/src/security/authenticode.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ use std::{
99
mem::size_of,
1010
path::Path,
1111
ptr::{from_ref, null_mut},
12-
os::windows::ffi::OsStrExt,
1312
};
13+
#[cfg(windows)]
14+
use std::os::windows::ffi::OsStrExt;
15+
#[cfg(windows)]
1416
use windows::{
1517
core::{PCWSTR, PWSTR, GUID},
1618
Win32::{
@@ -32,19 +34,21 @@ use windows::{
3234
}
3335
}
3436
};
37+
#[cfg(windows)]
3538
use windows_result::HRESULT;
3639

3740
/// Check the Authenticode signature of a file.
38-
///
41+
///
3942
/// # Arguments
40-
///
43+
///
4144
/// * `file_path` - The path to the file to check.
42-
///
45+
///
4346
/// # Returns
44-
///
47+
///
4548
/// * `Ok(())` if the file is signed and the signature is valid.
4649
/// * `Err(DscError)` if the file is not signed or the signature is invalid
47-
///
50+
///
51+
#[cfg(windows)]
4852
pub fn check_authenticode(file_path: &Path) -> Result<(), DscError> {
4953
if is_file_checked(file_path) {
5054
return Ok(());

dsc_lib/src/security/mod.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
#[cfg(windows)]
45
use authenticode::check_authenticode;
5-
use std::{path::Path, sync::LazyLock};
6+
use std::path::Path;
7+
#[cfg(windows)]
8+
use std::sync::LazyLock;
69

710
use crate::dscerror::DscError;
811

912
#[cfg(windows)]
1013
mod authenticode;
1114

15+
#[cfg(windows)]
1216
static CHECKED_FILES: LazyLock<std::sync::Mutex<Vec<String>>> = LazyLock::new(|| std::sync::Mutex::new(vec![]));
1317

18+
#[cfg(windows)]
1419
fn add_file_as_checked(file_path: &Path) {
1520
let file_str = file_path.to_string_lossy().to_string();
1621
let mut checked_files = CHECKED_FILES.lock().unwrap();
@@ -19,6 +24,7 @@ fn add_file_as_checked(file_path: &Path) {
1924
}
2025
}
2126

27+
#[cfg(windows)]
2228
fn is_file_checked(file_path: &Path) -> bool {
2329
let file_str = file_path.to_string_lossy().to_string();
2430
let checked_files = CHECKED_FILES.lock().unwrap();
@@ -28,26 +34,25 @@ fn is_file_checked(file_path: &Path) -> bool {
2834
/// Check the security of a file.
2935
///
3036
/// # Arguments
31-
///
37+
///
3238
/// * `file_path` - The path to the file to check.
33-
///
39+
///
3440
/// # Returns
35-
///
41+
///
3642
/// * `Ok(())` if the file passes the security checks.
3743
/// * `Err(DscError)` if the file fails the security checks.
38-
///
44+
///
3945
/// # Errors
40-
///
46+
///
4147
/// This function will return an error if the Authenticode check fails on Windows.
48+
#[cfg(windows)]
4249
pub fn check_file_security(file_path: &Path) -> Result<(), DscError> {
43-
#[cfg(windows)]
44-
{
45-
check_authenticode(file_path)?;
46-
Ok(())
47-
}
48-
#[cfg(not(windows))]
49-
{
50-
// On non-Windows platforms, we skip the Authenticode check.
51-
Ok(())
52-
}
50+
check_authenticode(file_path)?;
51+
Ok(())
52+
}
53+
54+
/// On non-Windows platforms, this function is a no-op.
55+
#[cfg(not(windows))]
56+
pub fn check_file_security(_file_path: &Path) -> Result<(), DscError> {
57+
Ok(())
5358
}

0 commit comments

Comments
 (0)