<commit_before>/*
 * This file is part of clr-boot-manager.
 *
 * Copyright © 2016 Intel Corporation
 *
 * clr-boot-manager is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 */

#define _GNU_SOURCE

#include <blkid.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <glob.h>
#include <libgen.h>
#include <limits.h>
#include <openssl/sha.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <unistd.h>

#include "files.h"
#include "log.h"
#include "nica/files.h"
#include "util.h"

DEF_AUTOFREE(DIR, closedir)

/**
 * Legacy boot bit, i.e. partition flag on a GPT disk
 */
#define CBM_MBR_BOOT_FLAG (1ULL << 2)

/**
 * By default we call sync() - for testing however we disable this due to timeout
 * issues.
 */
static bool cbm_should_sync = true;

void cbm_sync(void)
{
        if (cbm_should_sync) {
                sync();
        }
}

char *get_sha1sum(const char *p)
{
        unsigned char hash[SHA_DIGEST_LENGTH] = { 0 };
        int fd = -1;
        struct stat st = { 0 };
        char *buffer = NULL;
        char *ret = NULL;
        size_t max_len = SHA_DIGEST_LENGTH * 2;
        size_t st_size;

        if ((fd = open(p, O_RDONLY)) < 0) {
                goto finish;
        }
        if (fstat(fd, &st) < 0) {
                goto finish;
        }

        st_size = (size_t)st.st_size;

        buffer = mmap(0, st_size, PROT_READ, MAP_PRIVATE, fd, 0);
        if (!buffer) {
                goto finish;
        }

        if (!SHA1((unsigned char *)buffer, st_size, hash)) {
                goto finish;
        }

        ret = calloc((max_len) + 1, sizeof(char));
        if (!ret) {
                goto finish;
        }

        for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
                snprintf(ret + (i * 2), max_len, "%02x", hash[i]);
        }
        ret[max_len] = '\0';

finish:
        if (fd >= 0) {
                close(fd);
        }
        if (buffer) {
                munmap(buffer, st_size);
        }
        return ret;
}

bool cbm_files_match(const char *p1, const char *p2)
{
        autofree(char) *h1 = NULL;
        autofree(char) *h2 = NULL;

        h1 = get_sha1sum(p1);
        if (!h1) {
                return false;
        }
        h2 = get_sha1sum(p2);
        if (!h2) {
                return false;
        }

        return streq(h1, h2);
}

char *get_part_uuid(const char *path)
{
        struct stat st = { 0 };
        autofree(char) *node = NULL;
        blkid_probe probe = NULL;
        const char *value = NULL;
        char *ret = NULL;

        if (stat(path, &st) != 0) {
                LOG_ERROR("Path does not exist: %s", path);
                return NULL;
        }

        if (major(st.st_dev) == 0) {
                LOG_ERROR("Invalid block device: %s", path);
                return NULL;
        }

        if (!asprintf(&node, "/dev/block/%u:%u", major(st.st_dev), minor(st.st_dev))) {
                DECLARE_OOM();
                return NULL;
        }
<commit_msg>bootman: Compare stream buffer in cbm_files_match<commit_after>/*
 * This file is part of clr-boot-manager.
 *
 * Copyright © 2016 Intel Corporation
 *
 * clr-boot-manager is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 */

#define _GNU_SOURCE

#include <blkid.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <glob.h>
#include <libgen.h>
#include <limits.h>
#include <openssl/sha.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <unistd.h>

#include "files.h"
#include "log.h"
#include "nica/files.h"
#include "util.h"

DEF_AUTOFREE(DIR, closedir)

/**
 * Legacy boot bit, i.e. partition flag on a GPT disk
 */
#define CBM_MBR_BOOT_FLAG (1ULL << 2)

/**
 * By default we call sync() - for testing however we disable this due to timeout
 * issues.
 */
static bool cbm_should_sync = true;

void cbm_sync(void)
{
        if (cbm_should_sync) {
                sync();
        }
}

char *get_sha1sum(const char *p)
{
        unsigned char hash[SHA_DIGEST_LENGTH] = { 0 };
        int fd = -1;
        struct stat st = { 0 };
        char *buffer = NULL;
        char *ret = NULL;
        size_t max_len = SHA_DIGEST_LENGTH * 2;
        size_t st_size;

        if ((fd = open(p, O_RDONLY)) < 0) {
                goto finish;
        }
        if (fstat(fd, &st) < 0) {
                goto finish;
        }

        st_size = (size_t)st.st_size;

        buffer = mmap(0, st_size, PROT_READ, MAP_PRIVATE, fd, 0);
        if (!buffer) {
                goto finish;
        }

        if (!SHA1((unsigned char *)buffer, st_size, hash)) {
                goto finish;
        }

        ret = calloc((max_len) + 1, sizeof(char));
        if (!ret) {
                goto finish;
        }

        for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
                snprintf(ret + (i * 2), max_len, "%02x", hash[i]);
        }
        ret[max_len] = '\0';

finish:
        if (fd >= 0) {
                close(fd);
        }
        if (buffer) {
                munmap(buffer, st_size);
        }
        return ret;
}

bool cbm_files_match(const char *p1, const char *p2)
{
        autofree(CbmMappedFile) *m1 = CBM_MAPPED_FILE_INIT;
        autofree(CbmMappedFile) *m2 = CBM_MAPPED_FILE_INIT;

        if (!cbm_mapped_file_open(p1, m1)) {
                return false;
        }

        if (!cbm_mapped_file_open(p2, m2)) {
                return false;
        }

        /* If the lengths are different they're clearly not the same file */
        if (m1->length != m2->length) {
                return false;
        }

        /* Compare both buffers */
        if (memcmp(m1->buffer, m2->buffer, m1->length) == 0) {
                return true;
        }
        return false;
}

char *get_part_uuid(const char *path)
{
        struct stat st = { 0 };
        autofree(char) *node = NULL;
        blkid_probe probe = NULL;
        const char *value = NULL;
        char *ret = NULL;

        if (stat(path, &st) != 0) {
                LOG_ERROR("Path does not exist: %s", path);
                return NULL;
        }

        if (major(st.st_dev) == 0) {
                LOG_ERROR("Invalid block device: %s", path);
                return NULL;
        }

        if (!asprintf(&node, "/dev/block/%u:%u", major(st.st_dev), minor(st.st_dev))) {
                DECLARE_OOM();
                return NULL;
        }

{'fraction_non_alphanumeric': 0.13157142857142856, 'fraction_numerical': 0.012, 'mean_word_length': 1.8645662847790507, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 46, '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': '3351088', 'n_tokens_mistral': 2498, 'n_tokens_neox': 2365, 'n_words': 654}