<commit_before>#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "common.h"
#include "mlockfile.h"

struct mlockfile *mlockfile_init(const char *path)
{
    struct mlockfile *f = malloc(sizeof(struct mlockfile));
    if (!f) {
        perror("mlockfile_init struct alloc");
        return (NULL);
    }

    int path_length = strlen(path) + 1;

    f->path = malloc(path_length);
    if (!(f->path)) {
        perror("mlockfile_init path alloc");
        free(f);
        return (NULL);
    }

    memcpy(f->path, path, path_length);
    f->fd = -1;
    f->mmappedsize = 0;
    f->mmapped = NULL;

    return (f);
}

void mlockfile_release(struct mlockfile *f)
{
    if (f->mmapped) {
        if (munlock(f->mmapped, f->mmappedsize) < 0)
            perror("mlockfile_release munlock");
        if (munmap(f->mmapped, f->mmappedsize) < 0)
            perror("mlockfile_release munmap");
    }
    if (f->fd > -1) {
        if (close(f->fd) < 0)
            perror("mlockfile_release close");
        LOG_DEBUG("closed fd %i for %s\n", f->fd, f->path);
    }

    free(f->path);
    free(f);
}

int mlockfile_lock(struct mlockfile *f)
{
    struct stat stats;
    char *mmapped;

    if (f->fd < 0) {
        f->fd = open(f->path, O_RDONLY);
        if (f->fd < 0) {
            perror("mlockfile_lock open");
            return (-1);
        }
        LOG_DEBUG("opened fd %i for %s\n", f->fd, f->path);
    }

    if (fstat(f->fd, &stats) < 0) {
        perror("mlockfile_lock stat");
        return (-2);
    }

    mmapped = mmap(NULL, stats.st_size,
                   PROT_READ, MAP_SHARED | MAP_FILE, f->fd, (off_t) 0);
    if (mmapped == MAP_FAILED) {
        perror("mlockfile_lock mmap");
        return (-3);
    }

    if (mlock(mmapped, stats.st_size) < 0) {
        perror("mlockfile_lock mlock");
        if (munmap(mmapped, stats.st_size) < 0)
            perror("mlockfile_lock mlock failure munmap");
        return (-4);
    }

    if (f->mmapped) {
        LOG_INFO("relocked %s (%liB->%liB)\n",
                 f->path, (long) f->mmappedsize, (long) stats.st_size);
        if (munlock(f->mmapped, f->mmappedsize) < 0)<commit_msg>src/mlockfile.c: show "bytes" instead of "B"<commit_after>#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "common.h"
#include "mlockfile.h"

struct mlockfile *mlockfile_init(const char *path)
{
    struct mlockfile *f = malloc(sizeof(struct mlockfile));
    if (!f) {
        perror("mlockfile_init struct alloc");
        return (NULL);
    }

    int path_length = strlen(path) + 1;

    f->path = malloc(path_length);
    if (!(f->path)) {
        perror("mlockfile_init path alloc");
        free(f);
        return (NULL);
    }

    memcpy(f->path, path, path_length);
    f->fd = -1;
    f->mmappedsize = 0;
    f->mmapped = NULL;

    return (f);
}

void mlockfile_release(struct mlockfile *f)
{
    if (f->mmapped) {
        if (munlock(f->mmapped, f->mmappedsize) < 0)
            perror("mlockfile_release munlock");
        if (munmap(f->mmapped, f->mmappedsize) < 0)
            perror("mlockfile_release munmap");
    }
    if (f->fd > -1) {
        if (close(f->fd) < 0)
            perror("mlockfile_release close");
        LOG_DEBUG("closed fd %i for %s\n", f->fd, f->path);
    }

    free(f->path);
    free(f);
}

int mlockfile_lock(struct mlockfile *f)
{
    struct stat stats;
    char *mmapped;

    if (f->fd < 0) {
        f->fd = open(f->path, O_RDONLY);
        if (f->fd < 0) {
            perror("mlockfile_lock open");
            return (-1);
        }
        LOG_DEBUG("opened fd %i for %s\n", f->fd, f->path);
    }

    if (fstat(f->fd, &stats) < 0) {
        perror("mlockfile_lock stat");
        return (-2);
    }

    mmapped = mmap(NULL, stats.st_size,
                   PROT_READ, MAP_SHARED | MAP_FILE, f->fd, (off_t) 0);
    if (mmapped == MAP_FAILED) {
        perror("mlockfile_lock mmap");
        return (-3);
    }

    if (mlock(mmapped, stats.st_size) < 0) {
        perror("mlockfile_lock mlock");
        if (munmap(mmapped, stats.st_size) < 0)
            perror("mlockfile_lock mlock failure munmap");
        return (-4);
    }

    if (f->mmapped) {
        LOG_INFO("relocked %s (%li -> %li bytes)\n",
                 f->path, (long) f->mmappedsize, (long) stats.st_size);
        if (munlock(f->mmapped, f->mmappedsize) < 0)
{'fraction_non_alphanumeric': 0.16729615640968673, 'fraction_numerical': 0.007998222617196179, 'mean_word_length': 2.2365204888569377, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 77, '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': '3167598', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1668, 'n_words': 358}