<commit_before>#include <fudge.h>
#include <abi.h>

#define BLOCKSIZE 512
#define OK 1
#define ERROR 0

struct request
{

    unsigned int source;
    unsigned int offset;
    unsigned int count;
    unsigned int blocksector;
    unsigned int blockoffset;
    unsigned int blockcount;
    unsigned int blockreads;

} requests[8];

static char blocks[BLOCKSIZE * 4];

unsigned int next(struct cpio_header *header, unsigned int offset)
{

    return offset + cpio_next(header);

}

static void request_init(struct request *request, unsigned int source, unsigned int offset, unsigned int count)
{

    request->source = source;
    request->offset = offset;
    request->count = count;
    request->blocksector = offset / BLOCKSIZE;
    request->blockoffset = offset % BLOCKSIZE;
    request->blockcount = ((BLOCKSIZE - 1) + request->blockoffset + request->count) / BLOCKSIZE;
    request->blockreads = 0;

}

static void request_send(struct request *request)
{

    struct {struct message_header header; struct event_blockrequest blockrequest;} message;

    message.blockrequest.sector = request->blocksector;
    message.blockrequest.count = request->blockcount;

    message_initheader(&message.header, EVENT_BLOCKREQUEST, sizeof (struct event_blockrequest));
    file_writeall(FILE_G1, &message, message.header.length);

}

static unsigned int request_poll(struct request *request)
{

    struct message message;

    while (channel_polleventsystem(EVENT_DATA, &message))
    {

        request->blockreads += buffer_write(blocks, BLOCKSIZE * 4, message.data.buffer, message_datasize(&message.header), request->blockreads * BLOCKSIZE) / BLOCKSIZE;

        if (request->blockreads == request->blockcount)
            return request->count;

    }

    return 0;

}

static unsigned int sendpoll(struct request *request, unsigned int source, unsigned int offset, unsigned int count)
{

    request_init(request, source, offset, count);
    request_send(request);

    return request_poll(request);

}

static void *getdata(struct request *request)
{

    return ((char *)blocks + request->blockoffset);

}

static unsigned int walk(unsigned int source, struct request *request, char *path)
{

    unsigned int length = ascii_lengthz(path);
    unsigned int offset = 0;

    while (sendpoll(request, source, offset, sizeof (struct cpio_header) + 1024))
    {

        struct cpio_header *header = getdata(request);

        if (!cpio_validate(header))
            break;

        if (header->namesize == 11)
        {

            if (buffer_match("TRAILER!!!", header + 1, 11))
                break;

        }

        if (header->namesize == length)
        {

            if (buffer_match(path, header + 1, length))
                return OK;

        }

        offset = next(header, request->offset);

    }

    return ERROR;

}

static void dowalk(unsigned int source, void *data)
{

    struct request *request = &requests[0];
    struct p9p_twalk *twalk = data;
    unsigned int status;

    file_link(FILE_G1);

    status = walk(source, request, (char *)(twalk + 1));

    file_unlink(FILE_G1);

    if (status == OK)
    {

        struct message message;

        p9p_mkrwalk(&message);
        channel_sendmessageto(source, &message);

    }

}

static void doread(unsigned int source, void *data)
{

    struct request *request = &requests[0];
    struct p9p_tread *tread = data;

    channel_sendvalue(EVENT_DATA, p9p_read4(tread->fid), 10, 0);

    file_link(FILE_G1);

    if (sendpoll(request, source, request->offset, sizeof (struct cpio_header) + 1024))
    {

        struct cpio_header *header = getdata(request);

        if (cpio_validate(header))
        {

            unsigned int count;

            if ((count = sendpoll(request, source, request->offset + cpio_filedata(header), cpio_filesize(header))))
            {

                struct message message;

                p9p_mkrread(&message, count, getdata(request));
                channel_sendmessageto(source, &message);

            }

        }

    }

    file_unlink(FILE_G1);

}

static void onmain(unsigned int source, void *mdata, unsigned int msize)
{

    file_link(FILE_G0);

    while (channel_process());

    file_unlink(FILE_G0);

}

static void onp9p(unsigned int source, void *mdata, unsigned int msize)
{

    struct event_p9p *p9p = mdata;

    switch (p9p->type[0])
    {

    case P9P_TWALK:
        dowalk(source, p9p + 1);

        break;

    case P9P_TREAD:
        doread(source, p9p + 1);

        break;

    default:
        channel_sendstring(EVENT_DATA, "Unknown message received!\n");

        break;

    }

}

void init(void)
{

    if (!file_walk2(FILE_G0, "system:service/fd0"))
        return;

    if (!file_walk2(FILE_G1, "system:block/if:0/data"))
        return;

    channel_bind(EVENT_MAIN, onmain);
    channel_bind(EVENT_P9P, onp9p);

}

<commit_msg>more<commit_after>#include <fudge.h>
#include <abi.h>

#define BLOCKSIZE 512
#define OK 1
#define ERROR 0

struct request
{

    unsigned int source;
    unsigned int offset;
    unsigned int count;
    unsigned int blocksector;
    unsigned int blockoffset;
    unsigned int blockcount;
    unsigned int blockreads;

} requests[8];

static char blocks[BLOCKSIZE * 4];

unsigned int next(struct cpio_header *header, unsigned int offset)
{

    return offset + cpio_next(header);

}

static void request_init(struct request *request, unsigned int source, unsigned int offset, unsigned int count)
{

    request->source = source;
    request->offset = offset;
    request->count = count;
    request->blocksector = offset / BLOCKSIZE;
    request->blockoffset = offset % BLOCKSIZE;
    request->blockcount = ((BLOCKSIZE - 1) + request->blockoffset + request->count) / BLOCKSIZE;
    request->blockreads = 0;

}

static void request_send(struct request *request)
{

    struct {struct message_header header; struct event_blockrequest blockrequest;} message;

    message.blockrequest.sector = request->blocksector;
    message.blockrequest.count = request->blockcount;

    message_initheader(&message.header, EVENT_BLOCKREQUEST, sizeof (struct event_blockrequest));
    file_writeall(FILE_G1, &message, message.header.length);

}

static unsigned int request_poll(struct request *request)
{

    struct message message;

    while (channel_polleventsystem(EVENT_DATA, &message))
    {

        request->blockreads += buffer_write(blocks, BLOCKSIZE * 4, message.data.buffer, message_datasize(&message.header), request->blockreads * BLOCKSIZE) / BLOCKSIZE;

        if (request->blockreads == request->blockcount)
            return request->count;

    }

    return 0;

}

static unsigned int sendpoll(struct request *request, unsigned int source, unsigned int offset, unsigned int count)
{

    request_init(request, source, offset, count);
    request_send(request);

    return request_poll(request);

}

static void *getdata(struct request *request)
{

    return ((char *)blocks + request->blockoffset);

}

static unsigned int walk(unsigned int source, struct request *request, unsigned int length, char *path)
{

    unsigned int lengthz = length + 1;
    unsigned int offset = 0;

    while (sendpoll(request, source, offset, sizeof (struct cpio_header) + 1024))
    {

        struct cpio_header *header = getdata(request);

        if (!cpio_validate(header))
            break;

        if (header->namesize == 11)
        {

            if (buffer_match("TRAILER!!!", header + 1, 11))
                break;

        }

        if (header->namesize == lengthz)
        {

            if (buffer_match(path, header + 1, lengthz))
                return OK;

        }

        offset = next(header, request->offset);

    }

    return ERROR;

}

static void dowalk(unsigned int source, void *data)
{

    struct request *request = &requests[0];
    struct p9p_twalk *twalk = data;
    unsigned int status;

    file_link(FILE_G1);

    status = walk(source, request, p9p_read2(twalk->nwname), (char *)(twalk + 1));

    file_unlink(FILE_G1);

    if (status == OK)
    {

        struct message message;

        p9p_mkrwalk(&message);
        channel_sendmessageto(source, &message);

    }

}

static void doread(unsigned int source, void *data)
{

    struct request *request = &requests[0];
    struct p9p_tread *tread = data;

    channel_sendvalue(EVENT_DATA, p9p_read4(tread->fid), 10, 0);

    file_link(FILE_G1);

    if (sendpoll(request, source, request->offset, sizeof (struct cpio_header) + 1024))
    {

        struct cpio_header *header = getdata(request);

        if (cpio_validate(header))
        {

            unsigned int count;

            if ((count = sendpoll(request, source, request->offset + cpio_filedata(header), cpio_filesize(header))))
            {

                struct message message;

                p9p_mkrread(&message, count, getdata(request));
                channel_sendmessageto(source, &message);

            }

        }

    }

    file_unlink(FILE_G1);

}

static void onmain(unsigned int source, void *mdata, unsigned int msize)
{

    file_link(FILE_G0);

    while (channel_process());

    file_unlink(FILE_G0);

}

static void onp9p(unsigned int source, void *mdata, unsigned int msize)
{

    struct event_p9p *p9p = mdata;

    switch (p9p->type[0])
    {

    case P9P_TWALK:
        dowalk(source, p9p + 1);

        break;

    case P9P_TREAD:
        doread(source, p9p + 1);

        break;

    default:
        channel_sendstring(EVENT_DATA, "Unknown message received!\n");

        break;

    }

}

void init(void)
{

    if (!file_walk2(FILE_G0, "system:service/fd0"))
        return;

    if (!file_walk2(FILE_G1, "system:block/if:0/data"))
        return;

    channel_bind(EVENT_MAIN, onmain);
    channel_bind(EVENT_P9P, onp9p);

}


{'fraction_non_alphanumeric': 0.10862913096695226, 'fraction_numerical': 0.01336189310485516, 'mean_word_length': 2.8970588235294117, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6111142', 'n_tokens_mistral': 3210, 'n_tokens_neox': 2962, 'n_words': 761}