Skip to content

Commit 84cdc69

Browse files
authored
Merge pull request #453 from rekka/no-digest-for-read-only
Do not compute the sha256 digest for read-only files
2 parents 3885446 + 760ec42 commit 84cdc69

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

src/io/cached_itarbundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ impl IoProvider for CachedITarBundle {
680680
Err(e) => return OpenResult::Err(e.into()),
681681
};
682682

683-
OpenResult::Ok(InputHandle::new(
683+
OpenResult::Ok(InputHandle::new_read_only(
684684
name,
685685
BufReader::new(f),
686686
InputOrigin::Other,

src/io/format_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl IoProvider for FormatCache {
8484
OpenResult::Err(e) => return OpenResult::Err(e),
8585
};
8686

87-
OpenResult::Ok(InputHandle::new(
87+
OpenResult::Ok(InputHandle::new_read_only(
8888
name,
8989
BufReader::new(f),
9090
InputOrigin::Other,

src/io/mod.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ pub enum InputOrigin {
7171
/// computation. The `ExecutionState` code then has further pieces that track
7272
/// access to nonexistent files, which we treat as being equivalent to an
7373
/// existing empty file for these purposes.
74-
7574
pub struct InputHandle {
7675
name: OsString,
7776
inner: Box<dyn InputFeatures>,
77+
/// Indicates that the file cannot be written to (provided by a read-only IoProvider) and
78+
/// therefore it is useless to compute the digest.
79+
read_only: bool,
7880
digest: digest::DigestComputer,
7981
origin: InputOrigin,
8082
ever_read: bool,
@@ -91,6 +93,24 @@ impl InputHandle {
9193
InputHandle {
9294
name: name.to_os_string(),
9395
inner: Box::new(inner),
96+
read_only: false,
97+
digest: Default::default(),
98+
origin,
99+
ever_read: false,
100+
did_unhandled_seek: false,
101+
ungetc_char: None,
102+
}
103+
}
104+
105+
pub fn new_read_only<T: 'static + InputFeatures>(
106+
name: &OsStr,
107+
inner: T,
108+
origin: InputOrigin,
109+
) -> InputHandle {
110+
InputHandle {
111+
name: name.to_os_string(),
112+
inner: Box::new(inner),
113+
read_only: true,
94114
digest: Default::default(),
95115
origin,
96116
ever_read: false,
@@ -114,13 +134,13 @@ impl InputHandle {
114134
}
115135

116136
/// Consumes the object and returns the SHA256 sum of the content that was
117-
/// written. No digest is returned if there was ever a seek on the input
137+
/// read. No digest is returned if there was ever a seek on the input
118138
/// stream, since in that case the results will not be reliable. We also
119139
/// return None if the stream was never read, which is another common
120140
/// TeX access pattern: files are opened, immediately closed, and then
121-
/// opened again.
141+
/// opened again. Finally, no digest is returned if the file is marked read-only.
122142
pub fn into_name_digest(self) -> (OsString, Option<DigestData>) {
123-
if self.did_unhandled_seek || !self.ever_read {
143+
if self.did_unhandled_seek || !self.ever_read || self.read_only {
124144
(self.name, None)
125145
} else {
126146
(self.name, Some(DigestData::from(self.digest)))
@@ -174,7 +194,9 @@ impl Read for InputHandle {
174194

175195
self.ever_read = true;
176196
let n = self.inner.read(buf)?;
177-
self.digest.input(&buf[..n]);
197+
if !self.read_only {
198+
self.digest.input(&buf[..n]);
199+
}
178200
Ok(n)
179201
}
180202
}

src/io/zipbundle.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ impl<R: Read + Seek> IoProvider for ZipBundle<R> {
6666
return OpenResult::Err(e.into());
6767
}
6868

69-
OpenResult::Ok(InputHandle::new(name, Cursor::new(buf), InputOrigin::Other))
69+
OpenResult::Ok(InputHandle::new_read_only(
70+
name,
71+
Cursor::new(buf),
72+
InputOrigin::Other,
73+
))
7074
}
7175
}
7276

0 commit comments

Comments
 (0)