Skip to content

Add --only-cached/-C command line flag #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/bin/tectonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,17 @@ fn inner(args: ArgMatches, config: PersistentConfig, status: &mut TermcolorStatu
}
}

let only_cached = args.is_present("only_cached");
if only_cached {
tt_note!(status, "using only cached resource files");
}
if let Some(p) = args.value_of("bundle") {
let zb = ctry!(ZipBundle::<File>::open(Path::new(&p)); "error opening bundle");
sess_builder.bundle(Box::new(zb));
} else if let Some(u) = args.value_of("web_bundle") {
sess_builder.bundle(Box::new(config.make_cached_url_provider(&u, status)?));
sess_builder.bundle(Box::new(config.make_cached_url_provider(&u, only_cached, status)?));
} else {
sess_builder.bundle(config.default_bundle(status)?);
sess_builder.bundle(config.default_bundle(only_cached, status)?);
}

let mut sess = sess_builder.create(status)?;
Expand Down Expand Up @@ -149,6 +153,10 @@ fn main() {
.value_name("URL")
.help("Use this URL find resource files instead of the default")
.takes_value(true))
.arg(Arg::with_name("only_cached")
.short("C")
.long("only-cached")
.help("Use only resource files cached locally"))
.arg(Arg::with_name("outfmt")
.long("outfmt")
.value_name("FORMAT")
Expand Down
7 changes: 4 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl PersistentConfig {
Ok(config)
}

pub fn make_cached_url_provider(&self, url: &str, status: &mut StatusBackend) -> Result<LocalCache<ITarBundle<HttpITarIoFactory>>> {
pub fn make_cached_url_provider(&self, url: &str, only_cached: bool, status: &mut StatusBackend) -> Result<LocalCache<ITarBundle<HttpITarIoFactory>>> {
let itb = ITarBundle::<HttpITarIoFactory>::new(url);

let mut url2digest_path = app_dir(AppDataType::UserCache, &::APP_INFO, "urls")?;
Expand All @@ -85,16 +85,17 @@ impl PersistentConfig {
&url2digest_path,
&app_dir(AppDataType::UserCache, &::APP_INFO, "manifests")?,
&app_dir(AppDataType::UserCache, &::APP_INFO, "files")?,
only_cached,
status
)
}

pub fn default_bundle(&self, status: &mut StatusBackend) -> Result<Box<Bundle>> {
pub fn default_bundle(&self, only_cached: bool, status: &mut StatusBackend) -> Result<Box<Bundle>> {
if self.default_bundles.len() != 1 {
return Err(ErrorKind::Msg("exactly one default_bundle item must be specified (for now)".to_owned()).into());
}

Ok(Box::new(self.make_cached_url_provider(&self.default_bundles[0].url, status)?))
Ok(Box::new(self.make_cached_url_provider(&self.default_bundles[0].url, only_cached, status)?))
}

pub fn format_cache_path(&self) -> Result<PathBuf> {
Expand Down
11 changes: 9 additions & 2 deletions src/io/local_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ pub struct LocalCache<B: Bundle> {
manifest_path: PathBuf,
data_path: PathBuf,
contents: HashMap<OsString,LocalCacheItem>,
only_cached: bool,
}


impl<B: Bundle> LocalCache<B> {
pub fn new(
mut backend: B, digest: &Path, manifest_base: &Path,
data: &Path, status: &mut StatusBackend
data: &Path, only_cached: bool, status: &mut StatusBackend
) -> Result<LocalCache<B>> {
// If the `digest` file exists, we assume that it is valid; this is
// *essential* so that we can use a URL as our default IoProvider
Expand Down Expand Up @@ -142,7 +143,8 @@ impl<B: Bundle> LocalCache<B> {
checked_digest: checked_digest,
manifest_path: manifest_path,
data_path: data.to_owned(),
contents: contents
contents: contents,
only_cached: only_cached,
})
}

Expand Down Expand Up @@ -225,6 +227,11 @@ impl<B: Bundle> LocalCache<B> {
};
}

// The file is not in the cache and we are asked not to try to fetch it.
if self.only_cached {
return OpenResult::NotAvailable;
}

// Bummer, we haven't seen this file before. We need to (try to) fetch
// the item from the backend, saving it to disk and calculating its
// digest ourselves, then enter it in the cache and in our manifest.
Expand Down