Skip to content

Commit

Permalink
watchmanctl: make audit run on Windows
Browse files Browse the repository at this point in the history
Summary: watchmanctl building was failing because audit wasn't written for Windows. This diff fixes that.

Reviewed By: genevievehelsel

Differential Revision: D30783366

fbshipit-source-id: d927d8e5e97b2b03f77f0ed59134f5a00aac81f6
  • Loading branch information
fanzeyi authored and facebook-github-bot committed Sep 9, 2021
1 parent 68aeb02 commit bc69f67
Showing 1 changed file with 67 additions and 26 deletions.
93 changes: 67 additions & 26 deletions watchman/cli/src/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
use ahash::AHashMap;
use jwalk::WalkDir;
use serde::Deserialize;
use std::convert::TryInto;
use std::io::ErrorKind;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
#[cfg(windows)]
use std::os::windows::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -51,6 +56,14 @@ fn is_cookie<T: AsRef<Path>>(name: T) -> bool {
.map_or(false, |s| s.starts_with(".watchman-cookie-"));
}

#[cfg(windows)]
/// Windows epoch starts at 1601-01-01 00:00:00 UTC. This function converts
/// Windows epoch time to Unix epoch time. 0x019DB1DED53E8000 is Windows epoch
/// fo 1970-01-01 00:00:00 UTC.
fn from_windows_epoch(epoch: i64) -> i64 {
(epoch - 0x019DB1DED53E8000) / 10_000_000
}

impl AuditCmd {
pub(crate) async fn run(&self) -> anyhow::Result<()> {
let client = Connector::new().connect().await?;
Expand Down Expand Up @@ -257,36 +270,64 @@ impl AuditCmd {

let mut diffs = Vec::new();

if *watchman_file.mode != u64::from(metadata.permissions().mode()) {
diffs.push(format!(
"watchman mode is {} vs. fs {}",
*watchman_file.mode,
metadata.permissions().mode()
));
}
#[cfg(unix)]
{
if *watchman_file.mode != u64::from(metadata.permissions().mode()) {
diffs.push(format!(
"watchman mode is {} vs. fs {}",
*watchman_file.mode,
metadata.permissions().mode()
));
}

if metadata.is_file() && *watchman_file.size != metadata.size() {
diffs.push(format!(
"watchman size is {} vs. fs {}",
*watchman_file.size,
metadata.len()
));
}
if metadata.is_file() && *watchman_file.size != metadata.size() {
diffs.push(format!(
"watchman size is {} vs. fs {}",
*watchman_file.size,
metadata.len()
));
}

if metadata.is_file() && *watchman_file.mtime != metadata.mtime() {
diffs.push(format!(
"watchman mtime is {} vs. fs {}",
*watchman_file.mtime,
metadata.mtime()
));
}

if metadata.is_file() && *watchman_file.mtime != metadata.mtime() {
diffs.push(format!(
"watchman mtime is {} vs. fs {}",
*watchman_file.mtime,
metadata.mtime()
));
if *watchman_file.ino != metadata.ino() {
diffs.push(format!(
"watchman ino is {} vs. fs {}",
*watchman_file.ino,
metadata.ino()
));
}
}

if *watchman_file.ino != metadata.ino() {
diffs.push(format!(
"watchman ino is {} vs. fs {}",
*watchman_file.ino,
metadata.ino()
));
#[cfg(windows)]
{
// TODO: Add permission bit check for Windows.

if metadata.is_file() && *watchman_file.size != metadata.file_size() {
diffs.push(format!(
"watchman size is {} vs. fs {}",
*watchman_file.size,
metadata.len()
));
}

if metadata.is_file() {
if let Ok(last_write_time) = metadata.last_write_time().try_into() {
let last_write_time = from_windows_epoch(last_write_time);
if *watchman_file.mtime != last_write_time {
diffs.push(format!(
"watchman mtime is {} vs. fs {}",
*watchman_file.mtime, last_write_time,
));
}
}
}
}

if !diffs.is_empty() {
Expand Down

0 comments on commit bc69f67

Please sign in to comment.