JFLib

plat/fs.rs#

Source: lib/plat/fs.rs

fs -- file system access for backup and restore

  stat(path) / lstat(path) -> Option<Meta>     kind, size, times (ns), mode / attributes,
                                                owner, device and file id, link count
  read_dir(path) -> Option<Vec<DirEntry>>      names and kinds ("." / ".." left out); None
                                               also when an error cut the listing short
  read_link / symlink(target, link, dir) / hard_link(existing, new)
  set_times(path, atime_ns, mtime_ns)          on the link itself, never its target
  set_mode(path, mode) (Linux) / set_attrs(path, attrs) (Windows) / set_owner (Linux)
  free_space(path) -> Option<(free, total)>    sync_dir(dir) (Linux: fsync of the directory)
  write_file_private(path, data)               owner-only file (0600 / protected DACL),
                                               fsync, atomic rename, directory sync; its
                                               sibling is always a new file of its own
  join(dir, name) / parent(path)               '' separates on Windows only (and in vfs
                                               names); on Linux and macOS it is a character
  mkdir_private(path)                          owner-only directory (with parents); an
                                               existing one must be a folder (no link) of
                                               this user (Windows: also SYSTEM's or the
                                               administrators') nobody else may write to
                                               (Windows: has any access to), else false;
                                               Linux, macOS: one others may only read is
                                               made owner-only
  local_zone_id() -> "Europe/Berlin" | "win:W. Europe Standard Time"
  long_path(path) (Windows): the \\?\ form every call here uses
  Dir                                          an open folder: entries below it reached
                                               relative to it, never through a link (the
                                               section "folders by handle")
  set_file_times / set_file_mode (File)        through the open file's own handle
  fd_path(File) (Linux)                        a name of the open file itself for a program
                                               this one starts (/proc/<pid>/fd/<n>)

Android (docs/ANDROID.md AN3): the Linux code (the kernel's calls through rt::os) on the names of
android_files.rs -- /config, /data, /cache, /tmp, /documents, /imports and the rest of "/" are the
app's storage, /system, /product, /proc and the other folders of the system's own their real
paths --, '\' separates as in those names; Dir opens component by component (never openat2:
SIGSYS); an absolute link target is kept as its real path and read back as its name; no hard
links (SELinux refuses apps every one: hard_link is false); local_zone_id from the property
persist.sys.timezone.

Windows: reparse points count as links only for symbolic links and junctions; other tags
(deduplication, cloud placeholders) are ordinary files whose data is read.  Files are
opened with backup semantics (directories, and backup privileges when the process holds
them).

vfs names ("scheme://path", vfs.rs) work too: a folder mount's name is its OS path (and
checked as one); mem://, bundles, ZIP archives and other mounts answer from the provider
(stat: kind, size, times; read_dir; set_times; write_file_private / mkdir_private as
write / mkdir).  They have no links (read_link None, symlink / hard_link false), owners,
modes or free space.
  • enum FileKind what a directory entry is

    • File
    • Dir
    • Symlink -- a symbolic link (Windows: also a junction)
    • Other -- devices, pipes, sockets
  • struct Meta the metadata of a file

    • kind: Option<FileKind>
    • size: u64
    • mtime_ns: i64 -- nanoseconds since 1970
    • atime_ns: i64
    • ctime_ns: i64 -- Linux: status change; Windows: creation
    • mode: u32 -- Linux: st_mode (type and permission bits); Windows: 0
    • uid: u32
    • gid: u32
    • attrs: u32 -- Windows: FILE_ATTRIBUTE_*; Linux: 0
    • dev: u64 -- device (Windows: volume serial number) and file id (inode / file index)
    • ino: u64
    • nlink: u32

impl Meta

  • fn is_dir(&self) -> bool

  • fn is_file(&self) -> bool

  • struct DirEntry

    • name: String
    • kind: FileKind
  • fn join(dir: &str, name: &str) -> String "a/b" + "c" with the platform separator (Linux, macOS: a '\' is part of a name; Android: '/', a '\' separates there too)

  • fn parent(path: &str) -> &str the parent directory of a path ("" for a bare name; Linux, macOS: a '\' is part of a name)

  • fn stat(path: &str) -> Option<Meta> the metadata of path, links followed; a vfs name: its folder mount's file, else the provider's kind, size and times (mtime also as ctime; Linux modes 0644 / 0755)

  • fn lstat(path: &str) -> Option<Meta> the metadata of path itself (a link is not followed); mounts without OS files have no links

  • fn read_dir(path: &str) -> Option<Vec<DirEntry>> the entries of a folder ("." / ".." left out)

  • fn set_times(path: &str, atime_ns: i64, mtime_ns: i64) -> bool access and modification times (ns since 1970), on a link itself, never its target; a mount: its provider's times (MemFs keeps them; bundles and ZIP archives are read-only)

  • fn set_mode(path: &str, mode: u32) -> bool permission bits (07777; Windows: nothing to do); a mount without OS files has none

  • fn set_attrs(path: &str, attrs: u32) -> bool FILE_ATTRIBUTE_* (read-only, hidden, system, archive ...; Linux: nothing to do)

  • fn set_owner(path: &str, uid: u32, gid: u32) -> bool owner and group (Linux; Windows: nothing to do)

  • fn free_space(path: &str) -> Option<(u64, u64)> (free, total) bytes of the volume of path; None for mounts without OS files

  • fn sync_dir(dir: &str) -> bool make a folder's changes durable (Linux: fsync of the directory); a mount without OS files: whether the folder exists

  • fn write_file_private(path: &str, data: &[u8]) -> bool an owner-only file (0600 / protected DACL), synced and renamed into place (the sibling NAME.tmpN is created new, never an existing file taken over); a mount without OS files: its write (memory of this process)

  • fn mkdir_private(path: &str) -> bool an owner-only folder (with parents; existing parents are taken as they are). An existing folder must be the folder itself (no link or junction), belong to this user (Windows: or to SYSTEM or the administrators) and let nobody else write to it (Windows: grant nobody else anything), else false: someone else's folder, made before the program, may hold planted files. Linux and macOS make one of this user that others may only read owner-only (0700).

  • struct Dir An open folder: what lies below it is reached relative to it and never through a link, so a folder on the way that is swapped for a link (or renamed) after a check cannot lead a privileged read, write, change or removal out of it.

    Dir::open(path)                   the folder as it is (a link to it is followed: the
                                      trusted start, e.g. a shared folder); OS paths and
                                      folder mounts only
    open_dir(rel)                     a folder below ("a/b"; no "..", nothing absolute) with no
                                      link on the way: Linux openat2 (RESOLVE_BENEATH |
                                      RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS), else openat
                                      with O_NOFOLLOW for every component (kernels before 5.6,
                                      a seccomp filter that refuses openat2; Android always:
                                      its filter may end the app on openat2)
    names                             one component: no separator, not "." or ".." (Windows:
                                      no ':' either); "" is the folder itself where noted
    lstat / read_link / read_dir      links are reported, never followed
    open_file(name)                   a regular file, read only; a link or anything else: None
    create_new(name)                  a new file (Linux, macOS 0600), never an existing name
    mkdir / remove / rename / symlink a link is removed or renamed as the link
    set_times / set_mode / set_attrs  on the entry itself, never on a link's target
    set_file_times / set_file_mode    through an open File's own handle (whatever its name
                                      has become meanwhile)

    macOS: openat with O_NOFOLLOW component by component, the *at calls on the folder. Windows: the native calls relative to an open folder -- NtCreateFile with OBJECT_ATTRIBUTES. RootDirectory = the folder's handle and FILE_OPEN_REPARSE_POINT, one name at a time (a folder on the way that is a junction, a symbolic link or another name surrogate is refused, one that is no folder too), renames to a name relative to the target folder's handle (FILE_RENAME_INFORMATION.RootDirectory), removals through the entry's own handle, links as reparse data set on the new entry (FSCTL_SET_REPARSE_POINT, what CreateSymbolicLinkW writes). What lies below an open folder is found through the handles, never through a path, so a folder on the way renamed or swapped for a junction -- also one another program holds open with delete access -- changes nothing, and no folder is locked against renaming or removal meanwhile. On a network share the server looks the names up by path itself. The manifest's rules (policy.rs) are checked on the path an entry has below the start.

impl Dir

  • fn open(path: &str) -> Option<Dir> the folder path as it is (a link to it is followed); None when it is no folder

  • fn path(&self) -> &str the path the folder was opened as (with the names below its start)

  • fn open_dir(&self, rel: &str) -> Option<Dir> a folder below this one ("a/b"): no component may be a link (or a junction), none may be ".." and the path may not be absolute

  • fn lstat(&self, name: &str) -> Option<Meta> the metadata of an entry itself ("": the folder), a link not followed

  • fn read_dir(&self) -> Option<Vec<DirEntry>> the entries of the folder ("." / ".." left out); None also when an error cut it short

  • fn open_file(&self, name: &str) -> Option<crate::File> a regular file, read only (never through a link; a folder, device or pipe: None)

  • fn create_new(&self, name: &str) -> Option<crate::File> a new file, read and write (Linux, macOS: 0600); an existing name, also a link: None

  • fn mkdir(&self, name: &str) -> bool a new folder (Linux, macOS: 0777 less the umask); false also when the name exists

  • fn remove(&self, name: &str) -> bool a file, a link (never what it points to) or an empty folder

  • fn rename(&self, name: &str, to: &Dir, to_name: &str) -> bool rename name to to_name in to (this folder or another Dir on the same volume), replacing an existing file there; a link is renamed as the link

  • fn set_times(&self, name: &str, atime_ns: i64, mtime_ns: i64) -> bool access and modification times (ns since 1970) of an entry itself ("": the folder)

  • fn set_mode(&self, name: &str, mode: u32) -> bool permission bits (07777) of an entry ("": the folder); a link is left alone (Windows: nothing to do)

  • fn set_attrs(&self, name: &str, attrs: u32) -> bool FILE_ATTRIBUTE_* of an entry ("": the folder; Linux, macOS: nothing to do)

  • fn set_file_times(f: &crate::File, atime_ns: i64, mtime_ns: i64) -> bool access and modification times of an open file, through its handle; a mount's stream: false

  • fn set_file_mode(f: &crate::File, mode: u32) -> bool permission bits (07777) of an open file, through its handle (Windows: nothing to do); a mount's stream: false

  • fn fd_path(f: &crate::File) -> Option<String> A path that leads a program of this user (one this process starts, such as QEMU given a disk file made through a Dir) to the open file f itself, whatever its name has become or was put in its place meanwhile. Linux: /proc/<this process>/fd/<n>, valid while f stays open and this process runs (keep f as long as the other program may open the path again); None on Windows and macOS and for a mount's stream.

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