Skip to content

Commit

Permalink
Merge pull request #28 from NidhiHemanth/master
Browse files Browse the repository at this point in the history
Detecting state_change status for all states
  • Loading branch information
sn99 committed Jun 19, 2023
2 parents 9d7c94b + 34286ef commit 7820553
Show file tree
Hide file tree
Showing 24 changed files with 577 additions and 176 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ default-target = "x86_64-pc-windows-msvc"

[target.'cfg(target_os = "windows")'.dependencies]
serde = "1.0.159"
wmi = "0.12.2"
wmi = { git = "https://github.com/NidhiHemanth/wmi-rs.git", rev = "bebdc1f969974181a76d54d1486e8602bc7e9720" }
tokio = { version = "1.28.1", features = ["full"] }
5 changes: 3 additions & 2 deletions src/bin/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ async fn main() {
//k.update(); // for synchronous update

// println!("{k:#?}");
k.startup_commands.update();
k.async_update().await;
// k.startup_commands.update();

println!("{:#?}", k.startup_commands);
println!("{:#?}", k);
}
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@
//! }
//! ```

pub use std::collections::hash_map::DefaultHasher;
pub use std::hash::{Hash, Hasher};

pub mod operating_system;
pub mod state;

pub use wmi::COMLibrary;

pub fn hash_vec<T: Hash>(vec: &[T]) -> u64 {
let mut hasher = DefaultHasher::new();
vec.hash(&mut hasher);
hasher.finish()
}

/// Macro to automatically make `update` and `async_update` for a given state field
#[macro_export]
macro_rules! update {
Expand All @@ -40,7 +49,17 @@ macro_rules! update {
let wmi_con = WMIConnection::new(com_con).unwrap();

self.last_updated = SystemTime::now();

let old_vec = self.$struct_field.clone();
self.$struct_field = wmi_con.query().unwrap();

if(self.$struct_field.len() != old_vec.len()) {
self.state_change = true;
} else if (crate::hash_vec(&(self.$struct_field)) != crate::hash_vec(&old_vec)) {
self.state_change = true;
} else {
self.state_change = false;
}
}

/// Update fields asynchronously
Expand All @@ -50,7 +69,17 @@ macro_rules! update {
let wmi_con = WMIConnection::new(com_con).unwrap();

self.last_updated = SystemTime::now();

let old_vec = self.$struct_field.clone();
self.$struct_field = wmi_con.async_query().await.unwrap();

if (self.$struct_field.len() != old_vec.len()) {
self.state_change = true;
} else if (crate::hash_vec(&(self.$struct_field)) != crate::hash_vec(&old_vec)) {
self.state_change = true;
} else {
self.state_change = false;
}
}
}

Expand All @@ -60,6 +89,7 @@ macro_rules! update {
$struct_name {
$struct_field: Default::default(),
last_updated: SystemTime::now(),
state_change: false,
}
}
}
Expand Down
36 changes: 28 additions & 8 deletions src/operating_system/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,65 @@ use std::time::SystemTime;
use wmi::{COMLibrary, WMIConnection, WMIDateTime};

/// Represents the state of Windows user's desktops
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
pub struct Desktops {
/// Sequence of windows Desktop states
pub desktops: Vec<Win32_Desktop>,
/// When was the record last updated
pub last_updated: SystemTime,
/// Signifies change in state
///
/// - TRUE : The state changed since last UPDATE
/// - FALSE : The state is the same as last UPDATE
pub state_change: bool,
}

update!(Desktops, desktops);

/// Represents the state of Windows Environment
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
pub struct Environments {
/// Sequence of windows Environment states
pub environments: Vec<Win32_Environment>,
/// When was the record last updated
pub last_updated: SystemTime,
/// Signifies change in state
///
/// - TRUE : The state changed since last UPDATE
/// - FALSE : The state is the same as last UPDATE
pub state_change: bool,
}

update!(Environments, environments);

/// Represents the state of Windows `TimeZone`
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
pub struct TimeZones {
/// Sequence of windows TimeZone states
pub timezones: Vec<Win32_TimeZone>,
/// When was the record last updated
pub last_updated: SystemTime,
/// Signifies change in state
///
/// - TRUE : The state changed since last UPDATE
/// - FALSE : The state is the same as last UPDATE
pub state_change: bool,
}

update!(TimeZones, timezones);

/// Represents the state of Windows User Desktops
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
pub struct UserDesktops {
/// user account and desktop settings that are specific to it
pub user_desktops: Vec<Win32_UserDesktop>,
/// When was the record last updated
pub last_updated: SystemTime,
/// Signifies change in state
///
/// - TRUE : The state changed since last UPDATE
/// - FALSE : The state is the same as last UPDATE
pub state_change: bool,
}

update!(UserDesktops, user_desktops);
Expand All @@ -61,7 +81,7 @@ update!(UserDesktops, user_desktops);
/// properties of this class can be modified by the user to customize the desktop.
///
/// <https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-desktop>
#[derive(Default, Deserialize, Serialize, Debug, Clone)]
#[derive(Default, Deserialize, Serialize, Debug, Clone, Hash)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
pub struct Win32_Desktop {
Expand Down Expand Up @@ -141,7 +161,7 @@ pub struct Win32_Desktop {
/// `HKEY_USERS\<user>\Environment`
///
/// <https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-environment>
#[derive(Default, Deserialize, Serialize, Debug, Clone)]
#[derive(Default, Deserialize, Serialize, Debug, Clone, Hash)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
pub struct Win32_Environment {
Expand Down Expand Up @@ -208,7 +228,7 @@ pub struct Win32_Environment {
/// which includes the changes required for transitioning to daylight saving time transition.
///
/// <https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-timezone>
#[derive(Default, Deserialize, Serialize, Debug, Clone)]
#[derive(Default, Deserialize, Serialize, Debug, Clone, Hash)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
pub struct Win32_TimeZone {
Expand Down Expand Up @@ -366,7 +386,7 @@ pub struct Win32_TimeZone {
/// are specific to it.
///
/// <https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-userdesktop>
#[derive(Default, Deserialize, Serialize, Debug, Clone)]
#[derive(Default, Deserialize, Serialize, Debug, Clone, Hash)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
pub struct Win32_UserDesktop {
Expand Down
9 changes: 7 additions & 2 deletions src/operating_system/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ use std::time::SystemTime;
use wmi::{COMLibrary, WMIConnection, WMIDateTime};

/// Represents the state of Windows Drivers
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
pub struct Drivers {
/// Sequence of Drivers based on when they were loaded in chronological order
pub drivers: Vec<Win32_SystemDriver>,
/// When was the record last updated
pub last_updated: SystemTime,
/// Signifies change in state
///
/// - TRUE : The state changed since last UPDATE
/// - FALSE : The state is the same as last UPDATE
pub state_change: bool,
}

update!(Drivers, drivers);

/// The `Win32_SystemDriver` WMI class represents a process on an operating system.
///
/// <https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-systemdriver>
#[derive(Default, Deserialize, Serialize, Debug, Clone)]
#[derive(Default, Deserialize, Serialize, Debug, Clone, Hash)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
pub struct Win32_SystemDriver {
Expand Down
18 changes: 14 additions & 4 deletions src/operating_system/event_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,33 @@ use std::time::SystemTime;
use wmi::{COMLibrary, WMIConnection, WMIDateTime};

/// Represents the state of Windows `NTEventlogFiles`
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
pub struct NTEventlogFiles {
/// Represents data stored in a Windows Event log file
pub nt_event_log_files: Vec<Win32_NTEventlogFile>,
/// When was the record last updated
pub last_updated: SystemTime,
/// Signifies change in state
///
/// - TRUE : The state changed since last UPDATE
/// - FALSE : The state is the same as last UPDATE
pub state_change: bool,
}

update!(NTEventlogFiles, nt_event_log_files);

/// Represents the state of Windows `NTLogEvents`
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
pub struct NTLogEvents {
/// Represents sequence of Windows `NTLogEvents`
pub nt_log_events: Vec<Win32_NTLogEvent>,
/// When was the record last updated
pub last_updated: SystemTime,
/// Signifies change in state
///
/// - TRUE : The state changed since last UPDATE
/// - FALSE : The state is the same as last UPDATE
pub state_change: bool,
}

update!(NTLogEvents, nt_log_events);
Expand All @@ -39,7 +49,7 @@ update!(NTLogEvents, nt_log_events);
/// events. The file is also known as the event log.
///
/// <https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa394225(v=vs.85)>
#[derive(Default, Deserialize, Serialize, Debug, Clone)]
#[derive(Default, Deserialize, Serialize, Debug, Clone, Hash)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
pub struct Win32_NTEventlogFile {
Expand Down Expand Up @@ -190,7 +200,7 @@ pub struct Win32_NTEventlogFile {
/// otherwise "Access Denied" is returned to the application.
///
/// <https://learn.microsoft.com/en-us/previous-versions/windows/desktop/eventlogprov/win32-ntlogevent>
#[derive(Default, Deserialize, Serialize, Debug, Clone)]
#[derive(Default, Deserialize, Serialize, Debug, Clone, Hash)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
pub struct Win32_NTLogEvent {
Expand Down
Loading

0 comments on commit 7820553

Please sign in to comment.