![]() |
Files and caches | ![]() |
This page describes the file routines of
System/Files/file.hpp
and
file.cpp
, the caches of
System/Misc/data_cache.cpp
, the persistent store of
System/Misc/persistent.cpp
and the generated files of
System/Files/make_file.cpp
. All of them take URLs (see URLs, resolution and concretization) and end up in the platform functions texmacs_fopen, texmacs_stat, … of the platform layers.
bool load_string (url u, string& s, bool fatal, bool lock= true)
Reads the whole file into s. Unless u is already a rooted name, it is first resolved with the filter "fr"; the read fails if this gives nothing or a directory. The concretized name is looked up in the file cache (below); otherwise the file is opened with texmacs_fopen (name, "r", lock), which also takes an exclusive lock on the file when lock is set. Files of at most 10000 bytes in the cached directories, and files which were already cached, are stored in the cache. As in the rest of the file layer, the result is true on failure; with fatal a failure raises an error instead. The empty URL is not an error: it gives the empty string.
bool save_string (url u, string s, bool fatal= false)
append_string (u, s, fatal) is the same with appending. tmfs URLs are passed to the Scheme save handler (save_to_server); they cannot be appended to. Other names are resolved with the empty filter, which picks the first candidate of a search path whether it exists or not, and written with texmacs_fopen in mode "w" or "a". The file cache is updated for small cached files, and the parent directory is declared out of date (declare_out_of_date), so that the directory and attribute caches are refreshed.
Tests each letter of the filter (see resolution). For web URLs, the file is downloaded and only d, l, w and x fail; tmfs URLs ask the Scheme permission handler for r and w; a ramdisc is tested through its temporary file. Local files are tested with the attributes returned by texmacs_stat, possibly from the attribute cache. On Windows, the test x accepts readable files with the suffixes exe, com and bat and adds .exe to names without one of these suffixes. is_regular, is_directory and is_symbolic_link are shortcuts.
The size and the modification time in seconds; \(- 1\) and the most negative int but one for web and tmfs URLs and for missing files.
Whether u1 was modified after u2.
The format of a file, from its suffix (suffix_to_format) or, for tmfs, from tmfs-format.
The temporary directory of the session, $TEXMACS_HOME_PATH/system/tmp/pid (the start time instead of the process id on 32-bit Windows), created on first use. Directories of processes which no longer run are removed at boot time (paths, directories and settings).
A fresh name tmp_randomsuffix in that directory. The file is not created.
The first name prefixipostfix which does not exist in dir, creating dir if needed.
Names no_name_i.tm in $TEXMACS_HOME_PATH/texts/scratch, used for new buffers.
A backup name in $TEXMACS_HOME_PATH/texts/backup, made of the base name of u, a hash of the whole URL and the suffix.
The sorted list of entries (including . and ..) of the directory u, resolved with the filter "dr"; possibly from the directory cache. error_flag is only set when the directory is actually opened: it is left untouched when the result comes from the cache or when u does not resolve, so callers must initialize it.
move renames, copy and append_to go through load_string and save_string or append_string (so file permissions are not copied).
Removes all readable regular files which match u (wildcards are allowed: remove (dir * url_wildcard ("*")) empties a directory). rmdir removes matching empty directories and rmdir_recursive a whole tree.
Creates a directory and its missing parents, with mode 0744; does nothing if u exists. change_mode sets the mode.
search_file_in (dir, name) searches a directory tree for a file name, caching directory listings for 10 seconds; search_file_upwards also searches the ancestors up to one of a list of stop names. search_sub_dirs lists all subdirectories (used for the style and text paths). grep, search_score and file_completions support searching in the documentation and file name completion.
system (cmd, u1, …) and eval_system (cmd, u1, …) append the concretized, shell quoted names to a command (programs, web requests, messages and timing).
Starting TeXmacs reads thousands of files below $TEXMACS_PATH: Scheme modules, style files, fonts, icons and documentation. To avoid most of the corresponding system calls, the file layer keeps several caches, which are saved in $TEXMACS_HOME_PATH/system/cache and reloaded at the next start.
The cache is a single hash table from pairs (buffer key) to trees, with the following buffers; the key is always a concretized file or directory name.
The contents of directories, as stored by read_directory.
The mode, modification time and size of files, as stored by the attribute routines, or #f for a file which does not exist.
The contents of small files.
The contents of small documentation files; only loaded when a documentation file is first read.
Locations of TrueType fonts, used by
Plugins/Freetype/tt_file.cpp
.
For each directory, the modification time it had when its entries were cached.
cache_set, cache_get, is_cached and cache_reset access the table; cache_set marks the buffer as changed.
Only files below a few fixed directories are cached, as decided by the predicates do_cache_dir, do_cache_stat, do_cache_stat_fail, do_cache_file and do_cache_doc:
directory listings and file attributes below $TEXMACS_PATH and the documentation path (attributes also below $TEXMACS_HOME_PATH/fonts);
missing files below the same directories, except style files (.ts), so that a new style file is found without clearing the cache;
contents of files below $TEXMACS_PATH and $TEXMACS_HOME_PATH/fonts, except style files, in the file cache, and of files below the documentation path in the documentation cache.
The tests compare string prefixes of concretized names; the three base directories are fixed by cache_initialize during the boot.
An entry for a file is used only if the directory containing it is up to date: is_up_to_date (dir) compares the current modification time of the directory with the one recorded in validate_cache.scm, records the new time if it differs, and memorizes the answer for the rest of the session. A directory whose time changed is thus out of date for the whole session, and its entries are reloaded from disk; it becomes up to date again at the next start. declare_out_of_date (dir) forces this after TeXmacs itself wrote into a directory, and is_recursively_up_to_date checks a whole tree.
cache_memorize () writes all changed buffers to their files. It is called by edit_interface_rep::update_menus, that is, after user actions, so the cache is saved regularly while TeXmacs runs. cache_refresh () empties the table and reloads the buffers other than the documentation cache. cache_initialize (), called by texmacs_entrypoint, determines the base directories, calls cache_refresh, and removes the font error files in $TEXMACS_HOME_PATH/fonts/error if a font directory changed. The file and documentation caches are stored as plain text, with entries separated by the line %-%-tm-cache-%-%; the other buffers are stored as Scheme tuples.
The same directory holds caches which are managed elsewhere: the style caches __style__… of
Data/Document/new_style.cpp
(see style and document DRDs), plugin_cache.scm of
kernel/texmacs/tm-plugins.scm
, and the persistent stores of the Scheme documentation tools (below). The command line options -delete-cache, -delete-style-cache, -delete-file-cache, -delete-doc-cache, -delete-font-cache and -delete-plugin-cache remove parts of it (the main program).
System/Misc/persistent.cpp
implements a simple key-value store of strings on disk, exported to Scheme as persistent-set, persistent-get, persistent-has?, persistent-remove and persistent-file-name (and persistent-ref in
kernel/boot/abbrevs.scm
). A store is a directory. The entries are distributed over files according to a hash of the key: a file holds at most 10 entries and 4096 bytes, after which it is replaced by a directory of up to 26 files a to z, selected by the next digit of the hash in base 26. Values are cached in memory once read. persistent_file_name (dir, suffix) allocates fresh file names in the subdirectory _ of a store, using a counter stored in the file _/_. The documentation tools use this to keep the collected Scheme and macro documentation (
doc/apidoc-collect.scm
).
make_file (cmd, data, args) (
System/Files/make_file.cpp
) produces a file in $TEXMACS_HOME_PATH/system/make whose name is a hash of the command and its arguments. The commands are CMD_GET_FROM_WEB and CMD_GET_FROM_SERVER (local copies of remote images, named with the suffix of the remote file) and CMD_APPLY_EFFECT (an image to which an effect has been applied, as PNG). A file hash.check next to the result records the modification times of the local arguments; the result is reused across sessions as long as these times do not change, and within a session the name is remembered in memory. Remote arguments are first fetched with recursive calls; tmfs://artwork/… images are taken from $TEXMACS_HOME_PATH/misc/… when they exist there.
Cached contents are only invalidated when the directory changes. Editing a file in place below $TEXMACS_PATH usually does not change the modification time of its directory, so TeXmacs keeps using the old contents of small files (and the old size and date) until a file is added to or removed from the directory, or until the cache is deleted with -delete-file-cache. This often surprises developers who edit Scheme files of the installation.
is_newer (u1, u2) returns false as soon as one of the two files has an entry in the attribute cache (System/Files/file.cpp:397-398, under a comment “why was this?”), without comparing anything. Since every test like exists creates such entries for files below $TEXMACS_PATH and the documentation path, url-newer? is practically always false there. For instance, after (url-exists? a) and (url-exists? b) on $TEXMACS_PATH/progs/init-texmacs.scm and $TEXMACS_PATH/LICENSE, (url-newer? a b) returns #f although the first file is more recent. This affects in particular the incremental update of web sites built from the documentation (
doc/tmweb.scm
).
The filter l never succeeds on Unix: the attributes come from texmacs_stat, which follows symbolic links (stat, not lstat), and the link_flag argument of the attribute routine is ignored. is_symbolic_link and url-link? therefore always return false (checked with a link to a regular file: url-link? gives #f, url-regular? gives #t).
Files are locked with an exclusive lock both for reading and for writing, so concurrent readers of the same file are serialized. When writing, the file is truncated by fopen before the lock is taken, so the lock does not protect a concurrent reader from seeing a truncated file.
A generated file whose arguments are remote is never refreshed: their entry in the .check file is the constant #f. A remote image which changes on the server keeps its old local copy until system/make is cleared.
remove only removes readable regular files: it resolves its argument with the default filter "fr". Use rmdir or rmdir_recursive for directories.