Skip to main content

Module virtual_files

Module virtual_files 

Source
Expand description

The in-memory virtual file store — TeX’s write-out/read-back round trip.

Real TeX documents constantly write auxiliary files and read them back: \openout/\write/\closeout streams, {filecontents} environments, fancyvrb-style {VerbatimOut} captures, memoir \writeverbatim, and then \input/\openin/\verbatiminput/\IfFileExists on the produced files. Our conversion never touches the user’s disk for these (the design requirement allows writing only into the DESTINATION directory); instead every virtual write lands in this store and every file read consults it before kpathsea. The store is the single owner of the VIRTUAL_FILES state key (Perl keeps one <name>_contents string value per file) — do not reach into that map elsewhere.

Contents live in ONE owned HashMap<name, contents> mutated in place, NOT as interned strings: the interner never frees, so re-pinning the whole file on every \write line (the earlier shape) grew the arena quadratically in the line count — a {VerbatimOut} left unterminated at end of input (fancyvrb \FV@Scan re-runs \write per empty read) walked the interner’s buffer offset past its u32 range and aborted on a get_unchecked precondition (witness: fancyvrb/fancyvrb-doc cut at the open {SideBySideExample}, eof1.tex in the batch-48 guards).

Scope is per-conversion GLOBAL state (Scope::Global), reset with the rest of State between runs.

Functions§

output_file_name
The name TeX writes an output file under. \openout completes a name without an extension to <name>.tex (tex.web §1374 “if cur_ext=”“ then cur_ext:=“.tex”“, tex.web:24928), and the extension is what follows the LAST . after the last / (web2c’s more_name); TeX Live reads a braced name {…} as its group’s content (scan_file_name_braced). Reads look for <name>.tex first as well (§537), so \openout\f=notes + \input{notes} meet in the store either way, while \IfFileExists{notes.tex} only finds the completed name. Every writer that stands in for \openout — the primitive, {filecontents} (latex.ltx:18998, :19023), expl3’s \iow_open:Nn for tcolorbox’s \tcbverbatimwrite, listings’ \lst@WFBegin (lstmisc.sty:61) — names its file through this. Perl stores the bare name (TeX_FileIO.pool.ltxml:120-126; KNOWN_PERL_ERRORS #279). Witness latex4wp (latexdemo.sty:97-101 {filecontents*}{democode} then \IfFileExists{democode.tex}: 98 of 105 words of its examples missing).
vfs_append_line
Append one line (newline-terminated) to a virtual file, creating it if absent — the \write-to-stream shape.
vfs_exists
Does a virtual file exist?
vfs_read
Read a virtual file’s full content.
vfs_remove
Drop a virtual file, so later reads fall through to the real file system (the LSP overlay retracts an editor buffer this way).
vfs_store
Create/overwrite a virtual file with content.