JFLib

plat/vfs.rs#

Source: lib/plat/vfs.rs

vfs -- one file namespace on every platform: "scheme://path" reaches a mounted provider,
every other name is a path of the OS (or of the browser's virtual files)

  mount(scheme, provider) / unmount(scheme) -> bool / mounted(scheme) -> bool
  provider(scheme) -> Option<Arc<dyn Provider>>   a mount's provider (to build an Overlay or
                               a Sub of app://, for example)
  route(name) -> Route         Os (no scheme), Vfs(provider, path) or Bad (a scheme name
                               with ".." or an invalid scheme); "config:/" and "config:" are
                               the root like "config://"
  os_path(name) -> Option<String>   the OS path behind a name of a folder mount (for APIs
                               that need a real file); an OS path comes back unchanged
  stat(name) -> Option<Stat>   kind, size, times of a name with a scheme (Provider::stat)
  display_name(name) -> String the name to show the user (content://: the document's
                               display name; else the last segment)
  app_id() / set_app_id(id)    the name of the app's own folders (build_rs.py: the app's
                               folder name; else the exe's name)
  app_dir(APP_CONFIG | APP_DATA | APP_CACHE | APP_LOG) -> Option<String>   the app's own
                               folders: Windows %APPDATA%/<id>, %LOCALAPPDATA%/<id>,
                               %LOCALAPPDATA%/<id>/Cache, %LOCALAPPDATA%/<id>/Logs; Linux
                               the XDG config / data / cache / state homes + /<id>; browser
                               /config, /data, /cache + /<id>, /data/<id>/Logs
  Built in, mounted on first use: mem:// (one MemFs per process), config://, data://,
  cache://, logs:// (DirFs on app_dir), app:// (the app's bundle; ui::main! / rt::main! install it,
  else empty), and the user's folders desktop://, documents://, downloads://, pictures://,
  music://, videos:// (DirFs on user_dir, dirs.rs; only what the permissions allow); on
  Android content:// (the documents the user chose in the system's picker: content.rs,
  ContentFs over the ContentResolver)
  Providers: MemFs, DirFs::new(root), Overlay::new(layers) (with whiteouts),
  Sub::new(provider, folder), Bundle::new(bytes) (JFA1, tools/assetpack.py); archive::ZipFs
  (lib/archive).  Provider: kind, read, list (+ write, mkdir, delete, rename, os_path, stat,
  set_times, open -> Opened / Stream)
  pack_bundle(files) -> Vec<u8>    a JFA1 bundle (tests, tools; build_rs.py packs with
                               tools/assetpack.py)

The public file functions (guarded.rs) route names with a scheme here: read_file,
write_file, file_exists(_checked), mkdir, delete, rename (inside one mount), dir_list and
trash (folder mounts); plat::fs (stat, read_dir, links, times ...: fs.rs) too.  File::open,
create_new, open_temp and FileLock::acquire work on every mount: folder mounts open the OS
file (os_path), MemFs and Bundle (and ZipFs) hand out streams (Provider::open: Opened), other
providers get the whole file read and written back; a FileLock of a mount without OS files
holds inside this process.  Paths inside a mount use '/', have no "." or empty segments and
never "..".  Writes create missing folders.  DirFs uses the checked OS functions, so the
app's permissions apply to it; the app's own folders are always allowed (policy.rs), the
user's folders need rules ($DOCUMENTS ...).

Overlay whiteouts: deleting or renaming what a lower layer has writes ".wh.NAME" into the top
layer; a whited-out folder made again gets ".wh..wh..opq" (no lower entries below it).  The
markers live in the top layer (a DirFs keeps them on disk) and are never listed.

JFA1 bundle (little endian): "JFA1", u32 count, u32 names_len; count entries of 20 bytes
sorted by name: u32 name_off, u16 name_len, u8 codec (0 stored, 1 LZ4 block), u8 0,
u32 data_off (from the bundle start), u32 stored_len, u32 size; the names; the data.
/// app_dir: the app's settings folder (config://)
APP_CONFIG: u32 = 0
/// app_dir: the app's data folder (data://)
APP_DATA: u32 = 1
/// app_dir: the app's cache folder (cache://)
APP_CACHE: u32 = 2
/// app_dir: the app's log folder (logs://)
APP_LOG: u32 = 3
  • struct Stat what [Provider::stat] reports

    • kind: u32 -- 1 file, 2 folder
    • size: u64
    • mtime_ns: i64 -- nanoseconds since 1970; 0 = unknown (bundles)
    • atime_ns: i64
  • trait Stream: Send + Sync an open file of a provider without OS files (File::open on mem://, app://, zip mounts); offsets as in plat::File

    • fn read_at(&self, off: u64, buf: &mut [u8]) -> Option<usize> bytes read at off (fewer at the end), None on an error
    • fn write_at(&self, _off: u64, _buf: &[u8]) -> Option<usize> bytes written at off (the file grows); None for a read-only stream
    • fn size(&self) -> Option<u64>
    • fn set_len(&self, _len: u64) -> bool cut or extend to len bytes
    • fn sync(&self) -> bool the data is where readers of the mount find it
    • fn deferred(&self) -> bool whether written bytes reach the provider only by sync or drop (a whole-file buffer: File on a provider without streams or OS files); plat::log refuses such files
  • trait Provider: Send + Sync A file tree behind a scheme. Paths are relative ("" is the root), '/' separated and normalised by route().

    • fn kind(&self, path: &str) -> u32 0 missing, 1 file, 2 folder
    • fn read(&self, path: &str) -> Option<Vec<u8>> the whole file
    • fn list(&self, path: &str, f: &mut dyn FnMut(&str, bool, u64)) -> bool f(name, is_dir, size) for every entry of a folder -> false when it is no folder
    • fn write(&self, _path: &str, _data: &[u8]) -> bool replace or create a file (missing folders are created)
    • fn mkdir(&self, _path: &str) -> bool create a folder and its parents (an existing folder is fine)
    • fn delete(&self, _path: &str) -> bool a file or an empty folder
    • fn rename(&self, _old: &str, _new: &str) -> bool a file to another name (replacing it)
    • fn os_path(&self, _path: &str) -> Option<String> the OS path of path (folder mounts only)
    • fn stat(&self, path: &str) -> Option<Stat> kind, size and times of path (None: missing); the default asks kind and the folder's listing, without times
    • fn set_times(&self, _path: &str, _atime_ns: i64, _mtime_ns: i64) -> bool set the access and modification times (ns since 1970) -> false where the provider keeps none
    • fn open(&self, _path: &str, _mode: u32) -> Opened File::open on the provider: a stream on the file (mode FO_READ, FO_WRITE: create / truncate, FO_RW: open or create). The default, Generic, lets File::open use os_path, else read and write the whole file (written back on sync and when the File is dropped)
    • fn create_new(&self, _path: &str) -> Opened a new file where nothing is (File::create_new, AtomicFile's sibling): the check and the creation in one step, so that no other writer's entry of that name is ever opened or emptied -> a stream for writing; Refused when the name is taken. The default, Generic: the OS's exclusive create of os_path; without os_path the provider has no File::create_new (AtomicFile then checks and opens, with a name of its own)
    • fn display_name(&self, _path: &str) -> Option<String> the name to show the user for path (None: its last segment; content:// documents: their display name)
    • fn atomic_rename(&self) -> bool whether a sibling renamed over a file replaces it in one step (AtomicFile commits so); false (content://): AtomicFile collects the bytes and writes the file in place
  • enum Opened what [Provider::open] answers

    • File(Box<dyn Stream>) -- the provider's own stream
    • Refused -- missing, read-only or refused: File::open fails
    • Generic -- no streams of its own: File::open uses os_path, else read / write of the whole file

impl Opened

  • fn of(f: Option<crate::File>) -> Opened a File (OS file or stream) as the answer, None as Refused

  • enum Route where a name leads

    • Os -- no scheme: a path of the OS
    • Vfs(Arc<dyn Provider>, String) -- a mounted provider and the path inside it
    • Bad -- a scheme name that cannot be used (".." or no such mount)
  • fn mount(scheme: &str, provider: Arc<dyn Provider>) -> bool mount provider under scheme (lower-case letters, digits, '+', '-', '.'; at least two characters); replaces an earlier mount -> false for an invalid scheme

  • fn unmount(scheme: &str) -> bool remove a mount -> whether there was one

  • fn mounted(scheme: &str) -> bool whether scheme is mounted (the built-in schemes count once they were used)

  • fn clean(path: &str) -> Option<String> the normalised path inside a mount ('\' -> '/', no "." / empty segments) or None (a "..")

  • fn route(name: &str) -> Route where name leads (built-in schemes are mounted on first use)

  • fn provider(scheme: &str) -> Option<Arc<dyn Provider>> the provider mounted under scheme (built-in schemes are mounted on first use)

  • fn os_path(name: &str) -> Option<String> the OS path behind name: a folder mount's file, or name itself when it has no scheme

  • fn stat(name: &str) -> Option<Stat> kind, size and times of a name with a scheme (Provider::stat; content://: what the document's provider reports) -> None for a missing one and for OS paths (plat::fs::stat)

  • fn display_name(name: &str) -> String the name to show the user for a file or folder: a content:// document's display name, else the last segment of the name ("C:/a/b.txt" -> "b.txt", "config://x/y" -> "y")

  • fn set_app_id(id: &str) set the name of the app's own folders (ui::main!, rt::main!: the app folder of build_rs.py); ignored unless it is a plain file name (letters, digits, '.', '-', '_', not only dots)

  • fn app_id() -> String the name of the app's own folders: set_app_id, else the exe's name without extension

  • fn app_dir(kind: u32) -> Option<String> the app's settings (APP_CONFIG), data (APP_DATA), cache (APP_CACHE) or log (APP_LOG) folder

  • struct MemFs files in memory (mem://, tests, generated data); open streams share the file's bytes like OS files: writes are seen at once, write / delete replace or drop the name only

impl MemFs

  • fn new() -> MemFs

  • struct DirFs an OS folder as the root (config://, data://, cache://, mounts of the app); every call goes through the checked OS functions, so the app's permissions apply

impl DirFs

  • fn new(root: &str) -> DirFs root: an absolute OS folder (created on the first write)

  • struct Overlay several providers in one tree: a path comes from the first layer that has it, a folder lists the entries of every layer, writes go to the first layer. Deleting or renaming what a lower layer has leaves a whiteout in the top layer (".wh.NAME"; a folder made again after that gets ".wh..wh..opq" and shows no lower entries), so a DirFs on top keeps them across runs; the markers are never listed, and names starting with ".wh." cannot be used.

impl Overlay

  • fn new(layers: Vec<Arc<dyn Provider>>) -> Overlay layers[0] is on top

  • struct Sub one folder of another provider as a root (a theme folder of app://)

impl Sub

  • fn new(inner: Arc<dyn Provider>, dir: &str) -> Option<Sub> dir: a folder of inner ('/' separated; None for "..")

  • struct Bundle a read-only JFA1 bundle (app://: the app's assets folder, packed by build_rs.py)

impl Bundle

  • fn empty() -> Bundle a bundle without files

  • fn from_static(b: &'static [u8]) -> Option<Bundle> the bundle embedded in the program (None: malformed)

  • fn new(b: Vec<u8>) -> Option<Bundle> a bundle in memory (None: malformed)

  • fn len(&self) -> usize the number of files

  • fn is_empty(&self) -> bool whether the bundle has no files

  • fn names(&self) -> Vec<&str> the paths of all files, sorted

  • fn pack_bundle(files: &[(&str, &[u8])]) -> Option<Vec<u8>> a JFA1 bundle of (name, data) pairs: LZ4 where that saves a tenth (tests, tools); None for a name route() would refuse, a name twice, or a bundle beyond 4 GiB

Source: docs/API.md in the JFLib repository.