Skip to content

Commit

Permalink
feat(chown): stop changing owner/group on unix platforms
Browse files Browse the repository at this point in the history
Fixes: #16

BREAKING CHANGE: If you were relying on the chown functionality for the index (which you probably weren't), then your index will no longer be updated that way.
  • Loading branch information
zkat committed Nov 7, 2019
1 parent 864c931 commit d5bb0df
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 121 deletions.
44 changes: 0 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@ serde = "1.0.92"
serde_derive = "1.0.92"
walkdir = "2.2.7"
either = "1.5.2"
mkdirp = "1.0.0"
futures-preview = "0.3.0-alpha.18"
async-std = { version = "0.99.10", features = ["unstable"]}
anyhow = "1.0.16"
thiserror = "1.0.3"

[target.'cfg(unix)'.dependencies]
chownr = "3.0.0"
nix = "0.14.0"

[dev-dependencies]
async-attributes = "1.0.0"
criterion = "0.2.11"
Expand Down
66 changes: 9 additions & 57 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result};
use async_std::{fs as afs, task};
#[cfg(unix)]
use chownr;
use async_std::fs as afs;
use digest::Digest;
use either::{Left, Right};
use futures::io::AsyncWriteExt;
use hex;
use mkdirp;
use serde_derive::{Deserialize, Serialize};
use serde_json::{json, Value};
use sha1::Sha1;
Expand Down Expand Up @@ -65,20 +62,7 @@ impl Hash for SerializableMetadata {

pub fn insert(cache: &Path, key: &str, opts: WriteOpts) -> Result<Integrity> {
let bucket = bucket_path(&cache, &key);
#[cfg(unix)]
{
if let Some(path) = mkdirp::mkdirp(bucket.parent().unwrap()).with_context(|| {
format!(
"Failed to create index bucket directory: {:?}",
bucket.parent().unwrap()
)
})? {
chownr::chownr(&path, opts.uid, opts.gid)
.with_context(|| format!("Failed to chown new index directories: {:?}", path))?;
}
}
#[cfg(windows)]
mkdirp::mkdirp(bucket.parent().unwrap()).with_context(|| {
fs::create_dir_all(bucket.parent().unwrap()).with_context(|| {
format!(
"Failed to create index bucket directory: {:?}",
bucket.parent().unwrap()
Expand All @@ -103,9 +87,6 @@ pub fn insert(cache: &Path, key: &str, opts: WriteOpts) -> Result<Integrity> {
buck.write_all(out.as_bytes())
.with_context(|| format!("Failed to write to index bucket at {:?}", bucket))?;
buck.flush()?;
#[cfg(unix)]
chownr::chownr(&bucket, opts.uid, opts.gid)
.with_context(|| format!("Failed to chown index bucket at {:?}", bucket))?;
Ok(opts
.sri
.or_else(|| "sha1-deadbeef".parse::<Integrity>().ok())
Expand All @@ -114,30 +95,12 @@ pub fn insert(cache: &Path, key: &str, opts: WriteOpts) -> Result<Integrity> {

pub async fn insert_async<'a>(cache: &'a Path, key: &'a str, opts: WriteOpts) -> Result<Integrity> {
let bucket = bucket_path(&cache, &key);
let tmpbucket = bucket.clone();
#[cfg(unix)]
let WriteOpts { uid, gid, .. } = opts;
task::spawn_blocking(move || {
let parent = tmpbucket.parent().unwrap();
#[cfg(unix)]
{
if let Some(path) = mkdirp::mkdirp(parent).with_context(|| {
format!("failed to create index bucket parent dir: {:?}", parent)
})? {
chownr::chownr(&path, uid, gid).with_context(|| {
format!(
"failed to change ownership for path {:?} to {:?}:{:?}",
path, uid, gid
)
})?;
}
}
#[cfg(windows)]
mkdirp::mkdirp(parent)
.with_context(|| format!("failed to create index bucket parent dir: {:?}", parent))?;
Ok::<(), anyhow::Error>(())
})
.await?;
afs::create_dir_all(bucket.parent().unwrap()).await.with_context(|| {
format!(
"Failed to create index bucket directory: {:?}",
bucket.parent().unwrap()
)
})?;
let stringified = serde_json::to_string(&SerializableMetadata {
key: key.to_owned(),
integrity: opts.sri.clone().map(|x| x.to_string()),
Expand All @@ -159,9 +122,6 @@ pub async fn insert_async<'a>(cache: &'a Path, key: &'a str, opts: WriteOpts) ->
.await
.with_context(|| format!("Failed to write to index bucket at {:?}", bucket))?;
buck.flush().await?;
#[cfg(unix)]
chownr::chownr(&bucket, opts.uid, opts.gid)
.with_context(|| format!("Failed to chown index bucket at {:?}", bucket))?;
Ok(opts
.sri
.or_else(|| "sha1-deadbeef".parse::<Integrity>().ok())
Expand Down Expand Up @@ -235,10 +195,6 @@ pub fn delete(cache: &Path, key: &str) -> Result<()> {
sri: None,
time: None,
metadata: None,
#[cfg(unix)]
uid: None,
#[cfg(unix)]
gid: None,
},
)
.map(|_| ())
Expand All @@ -254,10 +210,6 @@ pub async fn delete_async(cache: &Path, key: &str) -> Result<()> {
sri: None,
time: None,
metadata: None,
#[cfg(unix)]
uid: None,
#[cfg(unix)]
gid: None,
},
)
.map(|_| ())
Expand Down Expand Up @@ -423,7 +375,7 @@ mod tests {
let sri: Integrity = "sha1-deadbeef".parse().unwrap();
let time = 1_234_567;
let bucket = bucket_path(&dir, "hello");
mkdirp::mkdirp(bucket.parent().unwrap()).unwrap();
fs::create_dir_all(bucket.parent().unwrap()).unwrap();
fs::write(bucket, MOCK_ENTRY).unwrap();
let entry = find(&dir, "hello").unwrap().unwrap();
assert_eq!(
Expand Down
15 changes: 0 additions & 15 deletions src/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::pin::Pin;
use futures::prelude::*;

use anyhow::{Context, Result};
#[cfg(unix)]
use nix::unistd::{Gid, Uid};
use serde_json::Value;
use ssri::{Algorithm, Integrity};

Expand Down Expand Up @@ -202,10 +200,6 @@ pub struct WriteOpts {
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>,
}

impl WriteOpts {
Expand Down Expand Up @@ -285,15 +279,6 @@ impl WriteOpts {
self.sri = Some(sri);
self
}

/// 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;
self
}
}

/// A reference to an open file writing to the cache.
Expand Down

0 comments on commit d5bb0df

Please sign in to comment.