Skip to content

Commit 5397e4e

Browse files
authored
Merge pull request #884 from wischi-chr/customizable-cache-location
Make cache location customizable
2 parents 6da754d + cbd4d77 commit 5397e4e

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

crates/bundles/src/cache.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use fs2::FileExt;
2020
use std::{
2121
collections::HashMap,
22+
env,
2223
fs::{self, File},
2324
io::{BufRead, BufReader, Error as IoError, ErrorKind as IoErrorKind, Read, Write},
2425
path::{Path, PathBuf},
@@ -43,13 +44,26 @@ pub struct Cache {
4344
impl Cache {
4445
/// Get a handle to a bundle cache, using default per-user settings.
4546
///
47+
/// The cache location defaults to the `AppDataType::UserCache`
48+
/// provided by `app_dirs2` but can be overwritten using the
49+
/// `TECTONIC_CACHE_DIR` environment variable.
50+
///
4651
/// This method may perform I/O to create the user cache directory, so it is
4752
/// fallible. (Due to its `app_dirs2` implementation, it would have to be
4853
/// fallible even if it didn't perform I/O.)
4954
pub fn get_user_default() -> Result<Self> {
50-
Ok(Cache {
51-
root: app_dirs::ensure_user_cache_dir("")?,
52-
})
55+
let env_cache_path = env::var_os("TECTONIC_CACHE_DIR");
56+
57+
let cache_path = match env_cache_path {
58+
Some(env_cache_path) => {
59+
let env_cache_path = env_cache_path.into();
60+
fs::create_dir_all(&env_cache_path)?;
61+
env_cache_path
62+
}
63+
None => app_dirs::ensure_user_cache_dir("")?,
64+
};
65+
66+
Ok(Cache { root: cache_path })
5367
}
5468

5569
/// Get a handle to a bundle cache, using a custom cache directory.

docs/src/getting-started/first-document.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ $ tectonic -X build
105105

106106
If you haven’t run Tectonic on your computer before, this command will take a
107107
minute or two as it downloads the support files that it needs and generates the
108-
LaTeX “format file” storing the default macro collection. Tectonic will cache
108+
LaTeX “format file” storing the default macro collection. Tectonic will [cache](#cache)
109109
these files and avoid downloading them again. Test it out by running the build
110110
again:
111111

@@ -134,3 +134,18 @@ Tectonic’s “user experience” is substantially different from those engines
134134

135135
We hope that you’ll agree that these changes make for a program that is much
136136
more pleasant to use than the traditional tools.
137+
138+
139+
## Cache
140+
141+
The location of the cache depends on your operating system. You can use the
142+
[V2 Interface][v2cli-ref] to find the exact cache location on your machine
143+
or take a look [at the implementation][user-cache-impl].
144+
145+
If you need to change the location of the cache, you can do that by setting
146+
the environment variable `TECTONIC_CACHE_DIR` to the path of a directory.
147+
We recommend leaving the cache location at the default unless there is a
148+
compelling reason to change it.
149+
150+
[v2cli-ref]: ../ref/v2cli.md
151+
[user-cache-impl]: https://docs.rs/tectonic_io_base/latest/tectonic_io_base/app_dirs/fn.ensure_user_cache_dir.html

tests/cached_itarbundle.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use hyper::rt::Future;
66
use hyper::service::service_fn;
77
use hyper::{Body, Method, Request, Response, Server, StatusCode};
88
use std::collections::HashMap;
9-
use std::fs;
109
use std::io::{self, Write};
1110
use std::net::SocketAddr;
1211
use std::ops::Bound;
1312
use std::path::Path;
1413
use std::sync::{Arc, Mutex};
1514
use std::thread;
15+
use std::{env, fs};
1616
use tectonic::config::PersistentConfig;
1717
use tectonic::driver::ProcessingSessionBuilder;
1818
use tectonic::io::OpenResult;
@@ -459,3 +459,48 @@ fn test_bundle_update() {
459459
}
460460
});
461461
}
462+
463+
#[test]
464+
fn test_cache_location_redirect() {
465+
const CACHE_DIR_KEY: &str = "TECTONIC_CACHE_DIR";
466+
let tempdir = tempfile::tempdir().unwrap();
467+
468+
// In this test we intentionally set the environment variable and don't use the custom cache root parameter,
469+
// to test the internal mechanism for a custom cache location based on an environment variable.
470+
env::set_var(CACHE_DIR_KEY, tempdir.path().as_os_str());
471+
472+
let tar_index = {
473+
let mut builder = TarIndexBuilder::new();
474+
builder.push("plain.tex", b"simple").push(
475+
tectonic::digest::DIGEST_NAME,
476+
b"0000000000000000000000000000000000000000000000000000000000000000",
477+
);
478+
479+
builder.finish()
480+
};
481+
482+
run_test(Some(tar_index), |_, url| {
483+
let mut status = TermcolorStatusBackend::new(ChatterLevel::Minimal);
484+
let config = PersistentConfig::default();
485+
486+
let mut cache = config
487+
.make_cached_url_provider(url, false, None, &mut status)
488+
.unwrap();
489+
490+
match cache.input_open_name("plain.tex", &mut status) {
491+
OpenResult::Ok(_) => {}
492+
_ => panic!("Failed to open plain.tex"),
493+
}
494+
495+
// the filename of the target location is the SHA256 hash of the file content "simple"
496+
let expected_file_path = tempdir
497+
.path()
498+
.join("files")
499+
.join("a7")
500+
.join("a39b72f29718e653e73503210fbb597057b7a1c77d1fe321a1afcff041d4e1");
501+
502+
if !expected_file_path.exists() {
503+
panic!("Couldn't find the cached file in the expected location.");
504+
}
505+
});
506+
}

0 commit comments

Comments
 (0)