From c909a0837a83f0443a62c5b6226ee84b9bc27df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 31 Aug 2019 21:29:59 -0700 Subject: [PATCH] fix(windows): stop using chownr on windows --- Cargo.toml | 6 ++++-- src/errors.rs | 3 +++ src/index.rs | 13 +++++++++++-- src/put.rs | 4 ++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0dabc5e..29dad89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,13 +27,15 @@ digest = "0.8.0" serde_json = "1.0.39" serde = "1.0.92" serde_derive = "1.0.92" -nix = "0.14.0" -chownr = "2.0.0" failure = "0.1.5" walkdir = "2.2.7" either = "1.5.2" mkdirp = "1.0.0" +[target.'cfg(unix)'.dependencies] +chownr = "2.0.0" +nix = "0.14.0" + [dev-dependencies] criterion = "0.2.11" diff --git a/src/errors.rs b/src/errors.rs index 393801e..10baee3 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,6 @@ use std::io; +#[cfg(unix)] use chownr; use failure::Fail; use serde_json; @@ -24,6 +25,7 @@ pub enum Error { Io(#[fail(cause)] io::Error), /// Returned when there's an error with changing uid/gid on an entry. #[fail(display = "{}", _0)] + #[cfg(unix)] Chownr(#[fail(cause)] chownr::Error), /// Returned when there's an issue with metadata (de)serialization. #[fail(display = "{}", _0)] @@ -44,6 +46,7 @@ impl From for Error { } } +#[cfg(unix)] impl From for Error { fn from(error: chownr::Error) -> Self { Error::Chownr(error) diff --git a/src/index.rs b/src/index.rs index 38cdf52..fb3692e 100644 --- a/src/index.rs +++ b/src/index.rs @@ -5,6 +5,7 @@ use std::io::{ErrorKind, Write}; use std::path::{Path, PathBuf}; use std::time::{SystemTime, UNIX_EPOCH}; +#[cfg(unix)] use chownr; use digest::Digest; use either::{Left, Right}; @@ -62,9 +63,14 @@ impl Hash for SerializableEntry { pub fn insert(cache: &Path, key: &str, opts: PutOpts) -> Result { let bucket = bucket_path(&cache, &key); - if let Some(path) = mkdirp::mkdirp(bucket.parent().unwrap())? { - chownr::chownr(&path, opts.uid, opts.gid)?; + #[cfg(unix)] + { + if let Some(path) = mkdirp::mkdirp(bucket.parent().unwrap())? { + chownr::chownr(&path, opts.uid, opts.gid)?; + } } + #[cfg(windows)] + mkdirp::mkdirp(bucket.parent().unwrap())?; let stringified = serde_json::to_string(&SerializableEntry { key: key.to_owned(), integrity: opts.sri.clone().map(|x| x.to_string()), @@ -76,6 +82,7 @@ pub fn insert(cache: &Path, key: &str, opts: PutOpts) -> Result Result<(), Error> { sri: None, time: None, metadata: None, + #[cfg(unix)] uid: None, + #[cfg(unix)] gid: None, }, ) diff --git a/src/put.rs b/src/put.rs index f5268e0..5dfd01b 100644 --- a/src/put.rs +++ b/src/put.rs @@ -2,6 +2,7 @@ use std::io::prelude::*; use std::path::{Path, PathBuf}; +#[cfg(unix)] use nix::unistd::{Gid, Uid}; use serde_json::Value; use ssri::{Algorithm, Integrity}; @@ -32,7 +33,9 @@ pub struct PutOpts { pub(crate) size: Option, pub(crate) time: Option, pub(crate) metadata: Option, + #[cfg(unix)] pub(crate) uid: Option, + #[cfg(unix)] pub(crate) gid: Option, } @@ -97,6 +100,7 @@ impl PutOpts { /// Configures the uid and gid to write data as. Useful when dropping /// privileges while in `sudo` mode. + #[cfg(unix)] pub fn chown(mut self, uid: Option, gid: Option) -> Self { self.uid = uid; self.gid = gid;