Skip to content

Commit

Permalink
Support a BKT_TMPDIR environment variable in addition to the --cache-…
Browse files Browse the repository at this point in the history
…dir flag.
  • Loading branch information
dimo414 committed Nov 12, 2021
1 parent 8124347 commit 77c5d91
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,27 @@ $ bkt --scope=foo -- date +%s.%N
<a name="cache_dir"></a>
### Changing the Cache Directory

By default, cached data is stored under `/tmp` or a similar temporary directory,
this can be customized via the `--cache-dir` flag. Note that the choice of
directory can affect `bkt`'s performance; if the cache is stored under a
[`tmpfs`](https://en.wikipedia.org/wiki/Tmpfs) or solid-state partition it will
be significantly faster than caching to a spinning disk.
By default, cached data is stored under `/tmp` or a similar temporary directory;
this can be customized via the `--cache-dir` flag, or by setting the
`BKT_TMPDIR` environment variable. If both `BKT_TMPDIR` and `--cache-dir` are
used the flag `--cache-dir` will take priority.

Note that the choice of directory can affect `bkt`'s performance: if the cache
is stored under a [`tmpfs`](https://en.wikipedia.org/wiki/Tmpfs) or solid-state
partition it will be significantly faster than caching to a spinning disk.

## Security and Privacy

The default cache directory is potentially world-readable. On Unix the cache
directory is created with `700` permissions, meaning only the current user can
access it, but this is not foolproof.

You can customize the `--cache-dir` to a location you trust such as `~/.bkt`,
but note that your home directory may be slower than the `TMPDIR` (see
[above](#cache_dir)). In general, if you are not the only user of your system
it's wise to configure your `TMPDIR` to a location only you can access.
You can customize the cache directory (see [above](#cache_dir)) to a location
you trust such as `~/.bkt`, but note that your home directory may be slower than
the temporary directory selected by default.

In general, if you are not the only user of your system it's wise to configure
your `TMPDIR` to a location only you can access.

## Patterns and Tips

Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,17 @@ pub struct Bkt {
}

impl Bkt {
fn temp_dir() -> PathBuf {
std::env::var_os("BKT_TMPDIR").map(PathBuf::from).unwrap_or_else(std::env::temp_dir)
}

/// Creates a new Bkt instance using the [`std::env::temp_dir`] as the cache location.
///
/// # Errors
///
/// If preparing the tmp cache directory fails.
pub fn in_tmp() -> Result<Self> {
Bkt::create(std::env::temp_dir())
Bkt::create(Bkt::temp_dir())
}

/// Creates a new Bkt instance.
Expand Down
16 changes: 8 additions & 8 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn bkt<P: AsRef<Path>>(cache_dir: P) -> Command {
}
assert!(path.exists(), "Could not find bkt binary in {:?}", dir);
let mut bkt = Command::new(&path);
bkt.arg(format!("--cache-dir={}", cache_dir.as_ref().display()));
bkt.env("BKT_TMPDIR", cache_dir.as_ref().as_os_str());
bkt
}

Expand Down Expand Up @@ -191,11 +191,11 @@ fn respects_cache_dir() {
let file = dir.path("file");
let args = vec!("--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap());

let first_call = succeed(bkt(dir.path("cache")).args(&args));
let first_call = succeed(bkt(dir.path("cache")).arg(format!("--cache-dir={}", dir.path("cache").display())).args(&args));
assert_eq!(first_call, "1");
assert_eq!(first_call, succeed(bkt(dir.path("cache")).args(&args)));
assert_eq!(first_call, succeed(bkt(dir.path("cache")).arg(format!("--cache-dir={}", dir.path("cache").display())).args(&args)));

let diff_cache = succeed(bkt(dir.path("new-cache")).args(&args));
let diff_cache = succeed(bkt(dir.path("cache")).arg(format!("--cache-dir={}", dir.path("new-cache").display())).args(&args));
assert_eq!(diff_cache, "2");
}

Expand Down Expand Up @@ -368,14 +368,14 @@ fn force() {
let args = vec!("--", "bash", "-c", COUNT_INVOCATIONS, "arg0", file.to_str().unwrap());
let args_force = join(vec!("--force"), &args);

let output = succeed(bkt(&dir.path("cache")).args(&args));
let output = succeed(bkt(dir.path("cache")).args(&args));
assert_eq!(output, "1");
let output = succeed(bkt(&dir.path("cache")).args(&args));
let output = succeed(bkt(dir.path("cache")).args(&args));
assert_eq!(output, "1");

let output = succeed(bkt(&dir.path("cache")).args(&args_force));
let output = succeed(bkt(dir.path("cache")).args(&args_force));
assert_eq!(output, "2");
let output = succeed(bkt(&dir.path("cache")).args(&args));
let output = succeed(bkt(dir.path("cache")).args(&args));
assert_eq!(output, "2");
}

Expand Down

0 comments on commit 77c5d91

Please sign in to comment.