ratarmount

repository·master·Indexed 23 days ago

https://github.com/mxmlnkn/ratarmount

A tool for providing random access to archived resources (such as TAR, ZIP, and RAR files) by building indices and mounting them via FUSE. It allows users to browse and read files within compressed archives without full extraction, supporting recursive mounting, union mounting, and remote protocols via fsspec (HTTP, SFTP, S3, Dropbox, GitHub). The project includes the ratarmountcore library for FUSE integration and a CLI for mounting archives and managing cold storage metadata indices.

Tokens
12.7K
Snippets
22
Records
80
Agent score
29%

What's inside ratarmount

  1. What is Ratarmount?

    master

    Ratarmount is a tool that provides random access to archived resources (like TAR files) without requiring full extraction. It achieves this by collecting file positions and building indices containing seek points. It then mounts the archive using FUSE (via mfusepy), allowing you to browse and read files as if they were on a standard filesystem.

    Key capabilities include:

    • Random Access: Fast seeking inside compressed streams (bzip2, gzip, xz, zstd).
    • Highly Parallelized: Uses all available cores for decompression by default (controllable via -P <cores>).
    • Recursive Mounting: Automatically mounts nested archives (TARs inside TARs) into subdirectories.
    • Union/Bind Mounting: Supports merging multiple TARs, folders, or compressed files under a single mountpoint.
    • Write Overlay: Allows redirecting changes to a specific folder so deletions and modifications can be tracked and potentially applied back to the archive.
    • Remote Support: Mounts archives via URIs using fsspec (e.g., HTTP, SFTP, S3, Dropbox, GitHub).
  2. Understand ratarmount performance and behavior

    master

    Ratarmount is designed for high-performance random access to large TAR archives using FUSE. Key performance characteristics include:

    • Subsequent Mounts: Extremely fast (under 1 second) if a preexisting index sidecar file is available. Subsequent mounts avoid the heavy cost of initial index creation.
    • File Access: Reading file contents is significantly faster than alternatives like archivemount and does not scale poorly with archive size.
    • Memory Usage: Generally low and does not grow linearly with archive size. Note that the zstd backend uses mmap, and the gzip backend's memory usage grows with archive size.
    • Parallelism: Using the -P flag enables parallel decoding for bzip2 and xz archives, which can be significantly faster on multi-core processors.
    • Metadata Operations: Operations like find on the mount point may be slower than direct file reads due to the overhead of high-level FUSE interfaces.
    • Recursive Mounting: Unlike some alternatives, ratarmount supports mounting TAR files that are contained within other TAR files (recursive mounting).
  3. Performance improvements for `find` via `readdir` attributes

    master

    Since version 0.10.0, ratarmount has improved find performance by having the FUSE readdir implementation return file attributes (stat information) alongside file names.

    Why this matters: Previously, find had to perform a separate stat call for every file in a directory to determine if it was a file or a folder. By returning attributes during the readdir call, ratarmount reduces the number of FUSE callbacks significantly, speeding up directory traversal.

  4. Understand Zstd backend memory usage

    master

    The Zstd compression backend in ratarmount uses mmap to open archives. This results in a high reported memory overhead (e.g., 50GiB in benchmarks), but this memory is actually memory-mapped from the archive file.

    Key takeaway: This memory can be allocated by other processes if necessary and is not strictly 'used' in a way that triggers the OOM-killer. Old memory-mapped parts are automatically freed by the OS when more memory is required.

  5. How ratarmount manages its index

    master

    To enable fast subsequent mounts and low memory usage, ratarmount uses an index file created alongside the original TAR file. This index maps filenames to their metadata and their exact byte positions within the TAR.

    Key behaviors:

    • Persistence: The index is written to disk as it is created.
    • Scaling: Because the index is sorted by file path and name, lookups scale with $O(\log n)$.
    • Memory Efficiency: For large archives (>20k files), the memory footprint remains stable because the index is streamed to disk rather than held entirely in RAM.
  6. How MountSource implementations work

    master

    The ratarmountcore library provides a MountSource interface designed for FUSE integration. This interface allows for listing paths, retrieving file metadata, and accessing file contents.

    There are several implementations of MountSource categorized by their source type:

    Archive Implementations

    • SQLiteIndexedTar: The most powerful implementation; provides fast access to files inside (even compressed) TAR archives using an index.
    • RarMountSource: Uses rarfile to handle RAR archives.
    • ZipMountSource: Uses the built-in zipfile module for ZIP archives.
    • FolderMountSource: Treats an existing local folder as a mount source.

    Functional/Layered Implementations

    • UnionMountSource: Merges multiple MountSource implementations into a single, unified file hierarchy.
    • FileVersionLayer: A wrapper that accepts <file>.version/<number> paths and calls the underlying MountSource with the specified version.
    • AutoMountLayer: Recursively mounts archives found within a MountSource (similar to UnionMountSource).
  7. How the Metadata Index Cache works

    master

    To avoid the high cost of scanning a TAR archive every time it is mounted, ratarmount creates an index file containing file names, ownership, permissions, and offsets. This index is stored as a SQLite database.

    Default Search Order

    When mounting, ratarmount looks for an existing index in this order:

    1. <path to tar>.index.sqlite (a sidecar file in the same directory as the TAR).
    2. ~/.ratarmount/<sanitized_path>.index.sqlite (a fallback directory in the user's home folder).

    Customizing Index Locations

    You can override this behavior using the following options:

    • --index-folders: Specify a custom list of fallback folders to check for the index.
    • --index-file: Specify an explicitly named index file. Note: If you use --index-file, ratarmount will ignore all fallback folders and the default sidecar location.
  8. Optimize SQL Table Insertion via One-Time Sorting

    master

    To achieve high-performance metadata insertion, Ratarmount uses an intermediary unsorted table strategy. Instead of inserting directly into a table with a complex primary key (which incurs high overhead), it follows these steps:

    1. Create a temporary table (files_tmp) with a simple integer primary key. This allows for constant-time insertions.
    2. Insert all file metadata into this temporary table.
    3. Create the final files table with the required (path, name) primary key.
    4. Move data from the temporary table to the final table using INSERT INTO ... SELECT ... ORDER BY. Using ORDER BY during the transfer significantly improves performance by ensuring data is written to the final table in a sorted manner.
    5. Drop the temporary table.
    CREATE TABLE "files_tmp" (
        "id" INTEGER PRIMARY KEY,
        "path" VARCHAR(65535),
        "name" VARCHAR(65535)
    );
    INSERT INTO files VALUES
        (0,"abcdef", "ghijklmn"),
        (1,"opqrst","uvwxyz"),
        (2,"abcdef", "ghijklmn");
    CREATE TABLE "files" (
        "path" VARCHAR(65535),
        "name" VARCHAR(65535),
        PRIMARY KEY (path,name)
    );
    INSERT INTO "files" (path,name)
        SELECT path,name FROM "files_tmp"
        ORDER BY path,name;
    DROP TABLE "files_tmp";
  9. Access the mount point control interface

    master

    When a ratarmount instance is running, it creates a hidden .ratarmount-control folder within the mount point. This folder provides a way to interact with the process:

    • .ratarmount-control/output: Contains the errors and log output of the ratarmount process. Useful for background processes.
    • .ratarmount-control/command: You can write new command line invocations into this file to trigger a new ratarmount subprocess. Commands must start with ratarmount followed by a delimiter (space, newline, or null byte).
  10. Access hidden file versions via .versions folders

    master

    If a file exists multiple times in a TAR or across multiple mount sources, you can access the hidden versions through a special <file>.versions directory located at the mount point. The version numbers correspond to the tar --occurrence=N option.

    Example: If foo exists in a folder and as two versions in updated.tar, and you mount them as ratarmount folder updated.tar mountpoint:

    1. List versions: ls -la mountpoint/foo.versions/
    2. Access a specific version: cat mountpoint/foo.versions/1 (where 1 is the oldest version).
    ls -la mountpoint/foo.versions/
    cat mountpoint/foo.versions/1
  11. Ensure seekability for XZ and Zstandard files

    master

    Standard XZ and Zstandard files are often created as a single frame/block, which prevents true seeking. To use ratarmount's seeking capabilities effectively, you must use tools that create multi-frame/multi-block files.

    For XZ:

    • Use pixz to generate seekable XZ files.
    • Verify with xz -l <file> to check for multiple streams/blocks.

    For Zstandard (Zstd):

    • Use pzstd, zeekstd, t2sz, or zstd-seekable-format-go to create multi-frame files.
    • Verify with zstd -l <file> to ensure it contains more than one frame.

    Manual Multi-frame Zstd Creation: You can use the createMultiFrameZstd bash function to manually split a file, compress parts, and concatenate them into a seekable Zstd file.

  12. Understand ratarmount performance characteristics

    master

    When choosing between ratarmount and alternatives like archivemount, consider the following performance behaviors:

    • File Seek Time: ratarmount provides true seeking. Unlike some archive mounters that emulate seeking by reading all preceding data (scaling linearly with TAR size), ratarmount access time is constant regardless of file position. For bzip2 compressed TARs, it seeks directly to the bzip2 block using index information.
    • Memory Footprint: For TAR files with more than 20k files, ratarmount maintains a relatively constant memory footprint (approx. 30MB on some systems) because the index is written to disk during creation. In contrast, some alternatives keep the entire index in memory.
    • Mounting Time: ratarmount creates an index file next to the TAR file. Subsequent mounts are extremely fast because the index is reused. While initial mounting for very small files might be slower than some competitors, ratarmount scales much better for massive archives (e.g., 4M+ files).
    • Metadata Retrieval: Currently, listing files (e.g., using find) may be slower in ratarmount compared to C-based alternatives due to the underlying use of Python and SQLite.