Add new pristine subcommand

laumann
Jan 3, 2024, 7:53 PM
ZYBYX2TBKWAKNMPCWT3B7CJRDEO3LOENGOXZF27JGD4UH6YCN25AC

Dependencies

  • [2] SMB3M6ES change: rework to make more testable
  • [3] Q7TKZCJP Add initial support for reading the offsets from a (fixed) change
  • [4] XJ2PEH74 add meson.build
  • [5] XTKRT6OQ format the codebase
  • [6] L3HKOF4W wrap vendored zstd seekable in own file, #2
  • [7] YM2LC6QP wrap vendored blake3 in own file, #2
  • [8] ZPNA2D42 ani: add functions to locate .pijul
  • [9] B3XLVPNC Add ani.c and initial Makefile
  • [*] 67HZZ5HS meson: shuffle lib and exe sources
  • [*] PEUS54XQ
  • [*] FB67XX5E add argument parsing setup
  • [*] ZTDGWUGP add repository structure
  • [*] DRK7G22J Add header guard to commands.h

Change contents

  • replacement in meson.build at line 32
    [3.14][2.10185:10198]()
    'change.c'
    [3.14]
    [3.357]
    'change.c',
    'db.c',
  • edit in meson.build at line 46
    [11.124]
    [11.137]
    'cmd-pristine.c',
  • file addition: db.h (----------)
    [12.1]
    #ifndef ANI_DB_H
    #define ANI_DB_H
    struct globalheader {
    u16 version; // sanakirja version
    u8 root; // which page is currently the root page?
    u8 nroots; // how many root pages are there?
    u32 crc; // CRC of this page
    u64 length; // first free page at the end of the file
    u64 freedb; // offset of the free list
    u64 rcdb; // offset of the RC db
    };
    void dbrun(char *);
    #endif
  • file addition: db.c (----------)
    [12.1]
    #include <stdint.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdbool.h>
    #include <sys/mman.h>
    #include "common.h"
    #include "types.h"
    #include "scaffold.h"
    #include "db.h"
    #define err(msg) \
    do { \
    perror(msg); \
    exit(EXIT_FAILURE); \
    } while (0)
    static void
    dumpbytes(u8 *mem, usize len)
    {
    usize i;
    for (i = 0; i < len; i++) {
    printf("%02x ", mem[i]);
    }
    printf("\n");
    }
    static void
    readglobalheader(u8 *mem, struct globalheader *gh)
    {
    dumpbytes(mem, sizeof(struct globalheader));
    // FIXME: Does not work for big-endian systems
    *gh = *((struct globalheader *)mem);
    }
    static void
    printglobalheader(struct globalheader *gh, bool currentroot)
    {
    printf("version = %u root = %u %s nroots = %u\n", gh->version, gh->root,
    currentroot ? "(current)" : "", gh->nroots);
    printf("CRC = 0x%04x (%u)\n", gh->crc, gh->crc);
    printf("length = %lu (0x%08lx)\n", gh->length, gh->length);
    printf("freedb = %lu\n", gh->freedb);
    printf("rcdb = %lu\n", gh->rcdb);
    }
    void
    dbrun(char *db)
    {
    int fd;
    u8 *addr;
    u8 nroots, curroot;
    size i;
    struct globalheader gh;
    printf("opening %s\n", db);
    fd = open(db, O_RDONLY);
    if (fd == -1)
    err("open");
    usize length = 4096;
    int prot = PROT_READ; // memory protection; PROT_{EXEC,READ,WRITE,NONE}
    int flags = MAP_PRIVATE; // determines visibility
    off_t offset = 0;
    addr = mmap(NULL, length, prot, flags, fd, offset);
    if (addr == MAP_FAILED)
    err("mmap");
    readglobalheader(addr, &gh);
    munmap(addr, length);
    nroots = gh.nroots;
    curroot = gh.root;
    printglobalheader(&gh, curroot == 0);
    // for nroots-1 times, read the i << 12'th page
    for (i = 1; i < nroots; i++) {
    addr = mmap(NULL, length, prot, flags, fd, i << 12);
    if (addr == MAP_FAILED)
    err("mmap2");
    readglobalheader(addr, &gh);
    munmap(addr, length);
    printglobalheader(&gh, curroot == i);
    }
    close(fd);
    }
  • edit in commands.h at line 6
    [14.423]
    [15.40]
    int cmd_pristine(int, const char **, struct repository *);
  • file addition: cmd-pristine.c (----------)
    [12.1]
    #include <stdint.h>
    #include <stddef.h>
    #include <unistd.h>
    #include <string.h>
    #include "common.h"
    #include "types.h"
    #include "repository.h"
    #include "db.h"
    static void
    formatdbpath(char *dst, const char *repodir)
    {
    char *p;
    p = stpncpy(dst, repodir, PATH_MAX);
    *p++ = '/';
    p = stpncpy(p, DOTPIJUL, 6);
    *p++ = '/';
    p = stpncpy(p, "pristine/db", 11);
    }
    int
    cmd_pristine(int argc, char *argv, struct repository *repo)
    {
    char dbpath[4096] = { 0 };
    (void)argc;
    (void)argv;
    formatdbpath(dbpath, repo->path);
    dbrun(dbpath);
    return 0;
    }
  • replacement in ani.c at line 18
    [3.129][3.129:170](),[3.170][3.3154:3211]()
    " init Initialize empty repository\n"
    " change Show information about a particular change\n";
    [3.129]
    [3.173]
    " init Initialize empty repository\n"
    " change Show information about a particular change\n"
    " pristine Show information about the repository database\n";
  • edit in ani.c at line 32
    [3.1921]
    [13.1297]
    { "pristine", cmd_pristine, NEED_WORK_TREE },