Add initial support for reading the offsets from a (fixed) change

laumann
Aug 12, 2022, 8:38 PM
Q7TKZCJP2Z75EICZYKCEZDHKGERSOKZGMTSU3UXETBHTF663T66AC

Dependencies

Change contents

  • edit in commands.h at line 2
    [2.1089]
    int cmd_change(int argc, const char *argv[]);
  • file addition: change.h (----------)
    [4.1]
    #ifndef CHANGE_H
    #define CHANGE_H
    /**
    * Required headers
    * * stdint.h
    */
    #define CHANGE_VERSION 6
    /**
    * A table of contents for a change, indicating where each section is, to allow
    * seeking inside a change file.
    */
    struct offsets {
    uint64_t version;
    uint64_t hashed_len; /* length of hashed contents */
    uint64_t unhashed_off;
    uint64_t unhashed_len; /* length of unhashed contents */
    uint64_t contents_off;
    uint64_t contents_len; /* length of contents (uncompressed) */
    uint64_t total; /* contents_off + length(compressed contents) */
    };
    /* sizeof(struct offsets) */
    #define OFFSETS_SIZE 56
    /**
    * The possible errors that can arise when working with change files.
    */
    enum change_err {
    VERMISMATCH = -1
    };
    /**
    * Decode the 'struct offsets' from the given fd
    *
    * Return 0 if decoding completed successfully, a negative number indicates an
    * error.
    */
    int decode_offsets(int fd, struct offsets *);
    #endif
  • file addition: change.c (----------)
    [4.1]
    #include <stdint.h>
    #include <unistd.h> /* read() */
    #include <fcntl.h> /* open() */
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include "change.h"
    /** Read a uint64_t from byte buffer as little endian */
    #define legetu64(buf) (((uint64_t)buf[7]) << 56 | (uint64_t)buf[6] << 48 | (uint64_t)buf[5] << 40 | (uint64_t)buf[4] << 32 | (uint64_t)buf[3] << 24 | (uint64_t)buf[2] << 16 | (uint64_t)buf[1] << 8 | (uint64_t)buf[0]);
    int
    decode_offsets(int fd, struct offsets *off)
    {
    /* */
    ssize_t r;
    uint8_t buf[OFFSETS_SIZE];
    uint8_t *bufp;
    if ((r = read(fd, buf, OFFSETS_SIZE)) < OFFSETS_SIZE) {
    int err;
    if (r >= 0) {
    printf("error: not enough was read: %ld\n", r);
    err = -2;
    } else {
    printf("error: not enough was read: %ld\n", r);
    err = -1;
    }
    return err;
    }
    bufp = (uint8_t *)buf;
    off->version = legetu64(bufp);
    bufp += 8;
    off->hashed_len = legetu64(bufp)
    bufp += 8;
    off->unhashed_off = legetu64(bufp)
    bufp += 8;
    off->unhashed_len = legetu64(bufp)
    bufp += 8;
    off->contents_off = legetu64(bufp)
    bufp += 8;
    off->contents_len = legetu64(bufp)
    bufp += 8;
    off->total = legetu64(bufp)
    return 0;
    }
    int
    change()
    {
    int fd;
    int err;
    struct offsets off;
    /* FIXME: A very fixed example */
    fd = open(
    ".pijul/changes/AH/IXA5ZESN6QJXV2LKRSC4H5ZRFMJFHFPY2RQRARTH3XJLLCMCGQC.change",
    O_RDONLY
    );
    if (fd == -1) {
    printf("error: %s\n", strerror(errno));
    return -1;
    }
    err = decode_offsets(fd, &off);
    if (err != 0) {
    printf("error: failed to decode offsets\n");
    goto out;
    }
    printf("version : %lu\n", off.version);
    printf("hashed_len : %lu\n", off.hashed_len);
    printf("unhashed_off : %lu\n", off.unhashed_off);
    printf("unhashed_len : %lu\n", off.unhashed_len);
    printf("contents_off : %lu\n", off.contents_off);
    printf("contents_len : %lu\n", off.contents_len);
    printf("total : %lu\n", off.total);
    out:
    close(fd);
    return err;
    }
    int cmd_change(int argc, const char **argv)
    {
    /* FIXME: Add argument handling */
    return change();
    }
  • edit in ani.c at line 12
    [5.170]
    [5.170]
    " change Show information about a particular change\n"
  • edit in ani.c at line 15
    [5.174]
    [2.1173]
    /* List of builtin commands, by name and the function to invoke */
  • edit in ani.c at line 23
    [2.1297]
    [2.1297]
    { "change", cmd_change },
  • edit in Makefile at line 13
    [2.2019]
    [5.620]
    OBJS += change.o