Skip to content

fs

fs #

Filesystem facts the scratch reasons with: sizes, free space, network mounts, promotion.

NETWORK_FILESYSTEMS = frozenset({'9p', 'afs', 'beegfs', 'ceph', 'cifs', 'glusterfs', 'gpfs', 'lustre', 'ncpfs', 'smb3', 'smbfs', 'sshfs'}) module-attribute #

Filesystem types another machine serves, beside every nfs*; a fuse. prefix is ignored.

available_bytes(path) #

The bytes free to an unprivileged writer on path's filesystem.

Source code in capturegraph-lib/capturegraph/scratch/fs.py
def available_bytes(path: Path) -> int:
    """The bytes free to an unprivileged writer on ``path``'s filesystem."""
    stats = os.statvfs(path)
    return stats.f_bavail * stats.f_frsize

flush_tree(path) #

Write path and every file and directory under it through to the disk.

Source code in capturegraph-lib/capturegraph/scratch/fs.py
def flush_tree(path: Path) -> None:
    """Write ``path`` and every file and directory under it through to the disk."""
    entries = [path]
    for directory, subdirectories, files in os.walk(path):
        entries.extend(Path(directory, name) for name in (*subdirectories, *files))
    if len(entries) == 1:
        _flush(path)
        return
    with ThreadPoolExecutor(min(len(entries), _FLUSH_WORKERS)) as pool:
        list(pool.map(_flush, entries))

network_filesystem(path) #

The type of the network filesystem path is on, or None on a local disk.

Read from Linux's mount table; where there is none, every path counts as local.

Source code in capturegraph-lib/capturegraph/scratch/fs.py
def network_filesystem(path: Path) -> str | None:
    """The type of the network filesystem ``path`` is on, or ``None`` on a local disk.

    Read from Linux's mount table; where there is none, every path counts as local.
    """
    if not MOUNT_TABLE.exists():
        return None
    resolved = path.resolve()
    mounts = []
    for line in MOUNT_TABLE.read_text().splitlines():
        fields, _, source = line.partition(" - ")
        mount = Path(fields.split()[4].encode().decode("unicode_escape"))
        if resolved.is_relative_to(mount):
            mounts.append((len(mount.parts), source.split()[0]))
    if not mounts:
        return None
    _, kind = max(mounts)
    networked = kind.startswith("nfs") or kind.removeprefix("fuse.") in NETWORK_FILESYSTEMS
    return kind if networked else None

promote(source, destination) #

Move the tree source to destination atomically and durably, unless one is there.

source is on disk before the rename and the rename before this returns, so an index row the caller writes afterwards never names files a power loss can take back.

Returns:

Type Description
bool

Whether source became destination; False means an equal tree

bool

was already present and source was discarded.

Source code in capturegraph-lib/capturegraph/scratch/fs.py
def promote(source: Path, destination: Path) -> bool:
    """Move the tree ``source`` to ``destination`` atomically and durably, unless one is there.

    ``source`` is on disk before the rename and the rename before this returns,
    so an index row the caller writes afterwards never names files a power loss
    can take back.

    Returns:
        Whether ``source`` became ``destination``; ``False`` means an equal tree
        was already present and ``source`` was discarded.
    """
    if destination.exists():
        remove_tree(source)
        return False
    flush_tree(source)
    try:
        os.rename(source, destination)
    except OSError as error:
        if error.errno in (errno.EEXIST, errno.ENOTEMPTY):
            remove_tree(source)
            return False
        raise
    _flush(destination.parent)
    return True

tree_size(path) #

The bytes under path (a file's own size, a directory's recursive total).

Source code in capturegraph-lib/capturegraph/scratch/fs.py
def tree_size(path: Path) -> int:
    """The bytes under ``path`` (a file's own size, a directory's recursive total)."""
    status = os.lstat(path)
    if not os.path.isdir(path):
        return status.st_size
    total = 0
    for directory, _, names in os.walk(path):
        for name in names:
            total += os.lstat(os.path.join(directory, name)).st_size
    return total