Skip to content

Commit

Permalink
fix(windows): add windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Sep 1, 2019
1 parent 25a1918 commit a3e3d41
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
11 changes: 9 additions & 2 deletions src/content/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ pub fn content_path(cache: &Path, sri: &Integrity) -> PathBuf {

#[cfg(test)]
mod tests {
use super::content_path;
use super::*;
use ssri::Integrity;
use std::path::Path;

#[test]
fn basic_test() {
let sri = Integrity::from(b"hello world");
let cpath = content_path(Path::new("~/.my-cache"), &sri);
let mut wanted = PathBuf::new();
wanted.push("~/.my-cache");
wanted.push(format!("content-v{}", CONTENT_VERSION));
wanted.push("sha256");
wanted.push("b9");
wanted.push("4d");
wanted.push("27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
assert_eq!(
cpath.to_str().unwrap(),
"~/.my-cache/content-v2/sha256/b9/4d/27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
wanted.to_str().unwrap()
);
}
}
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;

#[cfg(unix)]
use chownr;
use failure::Fail;
use serde_json;
Expand All @@ -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)]
Expand All @@ -44,6 +46,7 @@ impl From<std::io::Error> for Error {
}
}

#[cfg(unix)]
impl From<chownr::Error> for Error {
fn from(error: chownr::Error) -> Self {
Error::Chownr(error)
Expand Down
13 changes: 11 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -62,9 +63,14 @@ impl Hash for SerializableEntry {

pub fn insert(cache: &Path, key: &str, opts: PutOpts) -> Result<Integrity, Error> {
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()),
Expand All @@ -76,6 +82,7 @@ pub fn insert(cache: &Path, key: &str, opts: PutOpts) -> Result<Integrity, Error
let mut buck = OpenOptions::new().create(true).append(true).open(&bucket)?;

write!(buck, "\n{}\t{}", hash_entry(&stringified), stringified)?;
#[cfg(unix)]
chownr::chownr(&bucket, opts.uid, opts.gid)?;
Ok(opts
.sri
Expand Down Expand Up @@ -120,7 +127,9 @@ pub fn delete(cache: &Path, key: &str) -> Result<(), Error> {
sri: None,
time: None,
metadata: None,
#[cfg(unix)]
uid: None,
#[cfg(unix)]
gid: None,
},
)
Expand Down
4 changes: 4 additions & 0 deletions src/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -32,7 +33,9 @@ pub struct PutOpts {
pub(crate) size: Option<usize>,
pub(crate) time: Option<u128>,
pub(crate) metadata: Option<Value>,
#[cfg(unix)]
pub(crate) uid: Option<Uid>,
#[cfg(unix)]
pub(crate) gid: Option<Gid>,
}

Expand Down Expand Up @@ -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<Uid>, gid: Option<Gid>) -> Self {
self.uid = uid;
self.gid = gid;
Expand Down

0 comments on commit a3e3d41

Please sign in to comment.