aware-tectonic 0.16.10

A modernized, complete, embeddable TeX/LaTeX engine. Tectonic is forked from the XeTeX extension to the classic "Web2C" implementation of TeX and uses the TeXLive distribution of support files. This is the Aware Software fork of tectonic 0.16.9: identical to upstream except that its bundle crate (aware-tectonic-bundles) does not contact the network when the bundle cache is warm.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
use anyhow::{bail, Context, Result};
use regex::Regex;
use sha2::{Digest, Sha256};
use std::{
    cmp::Ordering,
    collections::HashMap,
    fmt::Display,
    fs::{self, File},
    io::{self, Cursor, Read, Write},
    iter::FromIterator,
    path::{Path, PathBuf},
    process::{Command, Stdio},
};
use tracing::{debug, error, info, trace, warn};
use walkdir::WalkDir;

use crate::v2cli::commands::bundle::create::BundleCreateCommand;

use super::{
    input::Input,
    spec::BundleSearchOrder,
    spec::{BundleInputSource, BundleSpec},
};

#[derive(Default)]
pub struct PickStatistics {
    /// Total number of files added from each source
    added: HashMap<String, usize>,

    /// Number of file conflicts
    conflicts: usize,

    /// Total number of files ignored
    ignored: usize,

    /// Total number of patches applied
    patch_applied: usize,

    /// Total number of patches found
    patch_found: usize,
}

impl PickStatistics {
    /// Returns a pretty status summary string
    pub fn make_string(&self) -> String {
        let mut output_string = format!(
            concat!(
                "=============== Summary ===============\n",
                "    file conflicts:       {}\n",
                "    files ignored:        {}\n",
                "    diffs applied/found:  {}/{}\n",
                "    =============================\n",
            ),
            self.conflicts, self.ignored, self.patch_applied, self.patch_found,
        );

        let mut sum = 0;
        for (source, count) in &self.added {
            let s = format!("{source} files: ");
            output_string.push_str(&format!("    {s}{}{count}\n", " ".repeat(22 - s.len())));
            sum += count;
        }
        output_string.push_str(&format!("    total files:          {sum}\n\n"));

        output_string.push_str(&"=".repeat(39).to_string());
        output_string
    }

    /// Did we find as many, fewer, or more patches than we applied?
    pub fn compare_patch_found_applied(&self) -> Ordering {
        self.patch_found.cmp(&self.patch_applied)
    }
}

struct FileListEntry {
    /// Path relative to content dir (does not start with a slash)
    path: PathBuf,
    hash: Option<String>,
}

impl Display for FileListEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        format!(
            "{} {}",
            match &self.hash {
                Some(s) => &s,
                None => "nohash",
            },
            self.path.to_str().unwrap(),
        )
        .fmt(f)
    }
}

pub struct FilePicker {
    /// This bundle specification's root directory.
    /// (i.e, where we found bundle.toml)
    bundle_dir: PathBuf,

    /// Where to place this bundle's files
    build_dir: PathBuf,

    /// This file picker's statistics
    pub stats: PickStatistics,

    /// All files we've picked so far.
    /// This map's keys are the `path` value of `FileListEntry`.
    filelist: HashMap<PathBuf, FileListEntry>,

    bundle_spec: BundleSpec,
}

impl FilePicker {
    /// Transform a search order file with shortcuts
    /// (bash-like brace expansion, like `/a/b/{tex,latex}/c`)
    /// into a plain list of strings.
    fn expand_search_line(s: &str) -> Result<Vec<String>> {
        if !(s.contains('{') || s.contains('}')) {
            return Ok(vec![s.to_owned()]);
        }

        let first = match s.find('{') {
            Some(x) => x,
            None => bail!("Bad search path format"),
        };

        let last = match s.find('}') {
            Some(x) => x,
            None => bail!("Bad search path format"),
        };

        let head = &s[..first];
        let mid = &s[first + 1..last];

        if mid.contains('{') || mid.contains('}') {
            // Mismatched or nested braces
            bail!("Bad search path format");
        }

        // We find the first brace, so only tail may have other expansions.
        let tail = Self::expand_search_line(&s[last + 1..s.len()])?;

        if mid.is_empty() {
            bail!("Bad search path format");
        }

        let mut output: Vec<String> = Vec::new();
        for m in mid.split(',') {
            for t in &tail {
                if m.is_empty() {
                    bail!("Bad search path format");
                }
                output.push(format!("{head}{m}{t}"));
            }
        }

        Ok(output)
    }

    /// Patch a file in-place.
    /// This should be done after calling `add_file`.
    fn apply_patch(
        &mut self,
        path: &Path,
        path_in_source: &Path,
        diffs: &HashMap<PathBuf, PathBuf>,
    ) -> Result<bool> {
        // Is this file patched?
        if !diffs.contains_key(path_in_source) {
            return Ok(false);
        }

        info!("patching `{}`", path_in_source.to_str().unwrap());

        self.stats.patch_applied += 1;

        // Discard first line of diff
        let diff_file = fs::read_to_string(&diffs[path_in_source]).unwrap();
        let (_, diff) = diff_file.split_once('\n').unwrap();

        // TODO: don't require `patch`
        let mut child = Command::new("patch")
            .arg("--quiet")
            .arg("--no-backup")
            .arg(path)
            .stdin(Stdio::piped())
            .spawn()
            .context("while spawning `patch`")?;

        let mut stdin = child.stdin.take().unwrap();
        stdin
            .write_all(diff.as_bytes())
            .context("while passing diff to `patch`")?;
        drop(stdin);
        child.wait().context("while waiting for `patch`")?;

        Ok(true)
    }

    /// Add a file into the file list.
    fn add_to_filelist(&mut self, path: PathBuf, file: Option<&Path>) -> Result<()> {
        trace!("adding `{path:?}` to file list");

        self.filelist.insert(
            path.clone(),
            FileListEntry {
                path: path.clone(),
                hash: match file {
                    None => None,
                    Some(f) => {
                        let mut hasher = Sha256::new();
                        let _ = std::io::copy(
                            &mut fs::File::open(f)
                                .with_context(|| format!("while computing hash of {path:?}"))?,
                            &mut hasher,
                        )?;
                        Some(
                            hasher
                                .finalize()
                                .iter()
                                .map(|b| format!("{b:02x}"))
                                .collect::<Vec<_>>()
                                .concat(),
                        )
                    }
                },
            },
        );

        Ok(())
    }

    /// Add a file to this picker's content directory
    fn add_file(
        &mut self,
        path_in_source: &Path,
        source: &str,
        file_content: &mut dyn Read,
        diffs: &HashMap<PathBuf, PathBuf>,
    ) -> Result<()> {
        let target_path = self
            .build_dir
            .join("content")
            .join(source)
            .join(path_in_source);

        // Path to this file, relative to content dir
        let rel = target_path
            .strip_prefix(self.build_dir.join("content"))
            .unwrap()
            .to_path_buf();

        trace!("adding {path_in_source:?} from source `{source}`");

        // Skip files that already exist
        if self.filelist.contains_key(&rel) {
            self.stats.conflicts += 1;
            warn!("{path_in_source:?} from source `{source}` already exists, skipping");
            return Ok(());
        }

        fs::create_dir_all(match target_path.parent() {
            Some(x) => x,
            None => bail!("couldn't get parent of target"),
        })
        .context("failed to create content directory")?;

        // Copy to content dir.
        let mut file = fs::File::create(&target_path)?;
        io::copy(file_content, &mut file).with_context(|| {
            format!("while writing file `{path_in_source:?}` from source `{source}`")
        })?;

        // Apply patch if one exists
        self.apply_patch(&target_path, path_in_source, diffs)
            .with_context(|| {
                format!("while patching `{path_in_source:?}` from source `{source}`")
            })?;

        self.add_to_filelist(rel, Some(&target_path))
            .with_context(|| {
                format!("while adding file `{path_in_source:?}` from source `{source}`")
            })?;

        Ok(())
    }
}

// Public methods
impl FilePicker {
    /// Create a new file picker working in build_dir
    pub fn new(bundle_spec: BundleSpec, build_dir: PathBuf, bundle_dir: PathBuf) -> Result<Self> {
        if !build_dir.is_dir() {
            bail!("build_dir is not a directory!")
        }

        if build_dir.read_dir()?.next().is_some() {
            bail!("build_dir is not empty!")
        }

        Ok(FilePicker {
            bundle_dir,
            build_dir,
            filelist: HashMap::new(),
            bundle_spec,
            stats: PickStatistics::default(),
        })
    }

    /// Iterate over this bundle's sources
    pub fn iter_sources(&self) -> impl Iterator<Item = &String> {
        self.bundle_spec.inputs.keys()
    }

    /// Add a directory of files to this bundle under `source_name`,
    /// applying patches and checking for replacements.
    pub fn add_source(&mut self, cli: &BundleCreateCommand, source: &str) -> Result<()> {
        info!("adding source `{source}`");

        let input = self.bundle_spec.inputs.get(source).unwrap().clone();
        let mut added = 0usize;

        // Load diff files
        let diffs = input
            .patch_dir
            .as_ref()
            .map(|x| -> Result<HashMap<PathBuf, PathBuf>> {
                let mut diffs = HashMap::new();

                for entry in WalkDir::new(self.bundle_dir.join(x)) {
                    // Only iterate files
                    let entry = entry?;
                    if !entry.file_type().is_file() {
                        continue;
                    }
                    let entry = entry.into_path();

                    // Only include files with a `.diff extension`
                    if entry.extension().map(|x| x != "diff").unwrap_or(true) {
                        continue;
                    }

                    // Read first line of diff to get target path
                    let diff_file = fs::read_to_string(&entry).unwrap();
                    let (target, _) = diff_file.split_once('\n').unwrap();

                    trace!(tectonic_log_source = "select", "adding diff {entry:?}");

                    for t in Self::expand_search_line(target)?
                        .into_iter()
                        .map(PathBuf::from)
                    {
                        if diffs.contains_key(&t) {
                            warn!("the target of diff {entry:?} conflicts with another, ignoring");
                            continue;
                        }

                        diffs.insert(t, entry.clone());
                        self.stats.patch_found += 1;
                    }
                }

                Ok(diffs)
            })
            .unwrap_or(Ok(HashMap::new()))?;

        // Load and compile ignore patterns
        let ignore_patterns = {
            // Global patterns
            let mut ignore = self
                .bundle_spec
                .bundle
                .ignore
                .as_ref()
                .map(|v| {
                    v.iter()
                        .map(|x| Regex::new(&format!("^{x}$")))
                        .collect::<Result<Vec<Regex>, regex::Error>>()
                })
                .unwrap_or(Ok(Vec::new()))?;

            // Input patterns
            ignore.extend(
                input
                    .ignore
                    .as_ref()
                    .map(|v| {
                        v.iter()
                            .map(|x| Regex::new(&format!("^/{source}/{x}$")))
                            .collect::<Result<Vec<Regex>, regex::Error>>()
                    })
                    .unwrap_or(Ok(Vec::new()))?,
            );

            ignore
        };

        let mut source_backend = match &input.source {
            BundleInputSource::Directory { path, .. } => Input::new_dir(self.bundle_dir.join(path)),
            BundleInputSource::Tarball {
                path,
                root_dir,
                hash,
            } => {
                let x = match Input::new_tarball(self.bundle_dir.join(path), root_dir.clone()) {
                    Ok(x) => x,
                    Err(e) => {
                        error!("could not add source `{source}` from tarball");
                        return Err(e);
                    }
                };
                let hash = hash.clone();
                self.add_file(
                    Path::new("TAR-SHA256SUM"),
                    source,
                    &mut Cursor::new(format!("{}\n", x.hash().unwrap())),
                    &HashMap::new(),
                )?;

                if x.hash().unwrap() != hash {
                    if cli.allow_hash_mismatch {
                        warn!("hash of tarball for source `{source}` doesn't match expected value");
                        warn!("expected: {}", hash);
                        warn!("got:      {}", x.hash().unwrap());
                    } else {
                        error!(
                            "hash of tarball for source `{source}` doesn't match expected value"
                        );
                        error!("expected: {}", hash);
                        error!("got:      {}", x.hash().unwrap());
                        bail!("hash of tarball for source `{source}` doesn't match expected value")
                    }
                }

                info!("OK, tar hash matches bundle config");
                x
            }
        };

        for x in source_backend.iter_files() {
            let (rel_file_path, mut read) = x?;

            let ignore = {
                let f = format!("/{source}/{rel_file_path}");
                let mut ignore = false;
                for pattern in &ignore_patterns {
                    if pattern.is_match(&f) {
                        ignore = true;
                        break;
                    }
                }
                ignore
            };

            // Skip ignored files
            if ignore {
                debug!(
                    "skipping file {rel_file_path:?} from source `{source}` because of ignore patterns"
                );
                self.stats.ignored += 1;
                continue;
            }

            // Debug info
            if self.filelist.len() % 1937 == 1936 {
                info!("selecting files ({source}, {})", self.filelist.len());
            }

            trace!("adding file {rel_file_path:?} from source `{source}`");

            self.add_file(Path::new(&rel_file_path), source, &mut read, &diffs)
                .with_context(|| format!("while adding file `{rel_file_path:?}`"))?;
            added += 1;
        }

        self.stats.added.insert(source.to_owned(), added);

        Ok(())
    }

    pub fn finish(&mut self, save_debug_files: bool) -> Result<()> {
        info!("writing auxillary files");

        // Save search specification
        let search = {
            let mut search = Vec::new();
            let path = self.build_dir.join("content/SEARCH");

            for s in &self.bundle_spec.bundle.search_order {
                match s {
                    BundleSearchOrder::Plain(s) => {
                        for i in Self::expand_search_line(s)? {
                            search.push(i);
                        }
                    }
                    BundleSearchOrder::Input { input } => {
                        let s = &self.bundle_spec.inputs.get(input).unwrap().search_order;
                        if let Some(s) = s {
                            for line in s {
                                for i in Self::expand_search_line(&format!("/{input}/{line}"))? {
                                    search.push(i);
                                }
                            }
                        } else {
                            for i in Self::expand_search_line(&format!("/{input}//"))? {
                                search.push(i);
                            }
                        }
                    }
                }
            }

            let mut file = File::create(&path).context("while writing SEARCH")?;
            for s in &search {
                writeln!(file, "{s}")?;
            }

            self.add_to_filelist(PathBuf::from("SEARCH"), Some(&path))?;

            search
        };

        {
            // These aren't hashed, but must be listed anyway.
            // The hash is generated from the filelist, so we must add these before hashing.
            self.add_to_filelist(PathBuf::from("SHA256SUM"), None)?;
            self.add_to_filelist(PathBuf::from("FILELIST"), None)?;

            let mut filelist_vec = Vec::from_iter(self.filelist.values());
            filelist_vec.sort_by(|a, b| a.path.cmp(&b.path));

            let filelist_path = self.build_dir.join("content/FILELIST");

            // Save FILELIST.
            let mut file = File::create(&filelist_path).context("while writing FILELIST")?;
            for entry in filelist_vec {
                writeln!(file, "{entry}")?;
            }

            // Compute and save hash
            let mut file = File::create(self.build_dir.join("content/SHA256SUM"))
                .context("while writing SHA256SUM")?;

            let mut hasher = Sha256::new();
            let _ = std::io::copy(&mut fs::File::open(&filelist_path)?, &mut hasher)?;
            let hash = hasher
                .finalize()
                .iter()
                .map(|b| format!("{b:02x}"))
                .collect::<Vec<_>>()
                .concat();

            writeln!(file, "{hash}")?;
        }

        if save_debug_files {
            // Generate search-report
            {
                let mut file = File::create(self.build_dir.join("search-report"))
                    .context("while writing search-report")?;
                for entry in WalkDir::new(self.build_dir.join("content")) {
                    let entry = entry?;
                    if !entry.file_type().is_dir() {
                        continue;
                    }
                    let entry = entry
                        .into_path()
                        .strip_prefix(self.build_dir.join("content"))
                        .unwrap()
                        .to_owned();
                    let entry = PathBuf::from("/").join(entry);

                    // Will this directory be searched?
                    let mut is_searched = false;
                    for rule in &search {
                        if rule.ends_with("//") {
                            // Match start of patent path
                            // (cutting off the last slash from)
                            if entry.starts_with(&rule[0..rule.len() - 1]) {
                                is_searched = true;
                                break;
                            }
                        } else {
                            // Match full parent path
                            if entry.to_str().unwrap() == rule {
                                is_searched = true;
                                break;
                            }
                        }
                    }

                    if !is_searched {
                        let s = entry.to_str().unwrap();
                        let t = s.matches('/').count();
                        writeln!(file, "{}{s}", "\t".repeat(t - 1))?;
                    }
                }
            }
        }
        Ok(())
    }
}