GNU TeXmacsUser manualReferenceAboutEnglish
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.

1.Reading and writing files

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.

2.File tests and attributes

is_of_type (u, filter)

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.

file_size (u), last_modified (u, cache_flag)

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.

is_newer (u1, u2)

Whether u1 was modified after u2.

file_format (u)

The format of a file, from its suffix (suffix_to_format) or, for tmfs, from tmfs-format.

3.Special names

url_temp_dir ()

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).

url_temp (suffix)

A fresh name tmp_randomsuffix in that directory. The file is not created.

url_numbered (dir, prefix, postfix)

The first name prefixipostfix which does not exist in dir, creating dir if needed.

url_scratch (), is_scratch (u)

Names no_name_i.tm in $TEXMACS_HOME_PATH/texts/scratch, used for new buffers.

url_backup (u), is_backup (u)

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.

4.Directories and file operations

read_directory (u, error_flag)

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, copy, append_to

move renames, copy and append_to go through load_string and save_string or append_string (so file permissions are not copied).

remove (u)

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.

mkdir (u)

Creates a directory and its missing parents, with mode 0744; does nothing if u exists. change_mode sets the mode.

Searching

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.

Shell helpers

system (cmd, u1, …) and eval_system (cmd, u1, …) append the concretized, shell quoted names to a command (programs, web requests, messages and timing).

5.The data cache

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.

5.1.Buffers

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.

dir_cache.scm

The contents of directories, as stored by read_directory.

stat_cache.scm

The mode, modification time and size of files, as stored by the attribute routines, or #f for a file which does not exist.

file_cache

The contents of small files.

doc_cache

The contents of small documentation files; only loaded when a documentation file is first read.

font_cache.scm

Locations of TrueType fonts, used by

Plugins/Freetype/tt_file.cpp

.

validate_cache.scm

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.

5.2.What is cached

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:

The tests compare string prefixes of concretized names; the three base directories are fixed by cache_initialize during the boot.

5.3.Validity

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.

5.4.Saving and loading

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.

5.5.Other files in the cache directory

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).

6.The persistent store

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

).

7.Generated files

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.

8.Pitfalls

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".