<commit_before>/*
 * This file is part of clr-boot-manager.
 *
 * Copyright © 2016-2017 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 <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>

#include "blkid_stub.h"
#include "files.h"
#include "log.h"
#include "probe.h"
#include "system_stub.h"
#include "util.h"

/**
 * Convert a sysfs dev file to the target device path
 */
static char *cbm_dev_file_to_devpath(const char *devfile)
{
        int fd = 0;
        unsigned int dev_major, dev_minor = 0;
        char read_buf[PATH_MAX] = { 0 };
        ssize_t size = -1;

        /* Read the dev file */
        fd = open(devfile, O_RDONLY | O_NOCTTY | O_CLOEXEC);

        if (fd < 0) {
                return NULL;
        }

        size = read(fd, read_buf, sizeof(read_buf));
        close(fd);
        if (size < 1) {
                return NULL;
        }

        if (sscanf(read_buf, "%u:%u", &dev_major, &dev_minor) != 2) {
                return NULL;
        }

        return cbm_system_devnode_to_devpath(makedev(dev_major, dev_minor));
}

static char *cbm_get_luks_uuid(const char *part)
{
        autofree(char) *npath = NULL;
        autofree(char) *dpath = NULL;
        glob_t glo = { 0 };
        blkid_probe blk_probe = NULL;
        char *ret = NULL;
        const char *value = NULL;
        const char *sys = cbm_system_get_sysfs_path();

        /* i.e. /sys/block/dm-1/slaves/dm-0/slaves/sdb1/dev */
        npath = string_printf("%s/block/%s/slaves/*/slaves/*/dev", sys, part);

        glob(npath, GLOB_DOOFFS, NULL, &glo);

        if (glo.gl_pathc < 1) {
                globfree(&glo);
                return NULL;
        }

        dpath = cbm_dev_file_to_devpath(glo.gl_pathv[0]);
        globfree(&glo);
        if (!dpath) {
                return NULL;
        }

        blk_probe = cbm_blkid_new_probe_from_filename(dpath);
        if (!blk_probe) {
                LOG_ERROR("Unable to probe %s", dpath);
                return NULL;
        }<commit_msg>clr-boot-manager: add support for non-lvm root encryption<commit_after>/*
 * This file is part of clr-boot-manager.
 *
 * Copyright © 2016-2017 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 <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>

#include "blkid_stub.h"
#include "files.h"
#include "log.h"
#include "probe.h"
#include "system_stub.h"
#include "util.h"

/**
 * Convert a sysfs dev file to the target device path
 */
static char *cbm_dev_file_to_devpath(const char *devfile)
{
        int fd = 0;
        unsigned int dev_major, dev_minor = 0;
        char read_buf[PATH_MAX] = { 0 };
        ssize_t size = -1;

        /* Read the dev file */
        fd = open(devfile, O_RDONLY | O_NOCTTY | O_CLOEXEC);

        if (fd < 0) {
                return NULL;
        }

        size = read(fd, read_buf, sizeof(read_buf));
        close(fd);
        if (size < 1) {
                return NULL;
        }

        if (sscanf(read_buf, "%u:%u", &dev_major, &dev_minor) != 2) {
                return NULL;
        }

        return cbm_system_devnode_to_devpath(makedev(dev_major, dev_minor));
}

static char *cbm_get_luks_uuid(const char *part)
{
        autofree(char) *npath = NULL;
        autofree(char) *dpath = NULL;
        glob_t glo = { 0 };
        blkid_probe blk_probe = NULL;
        char *ret = NULL;
        const char *value = NULL;
        const char *sys = cbm_system_get_sysfs_path();

        /* i.e. /sys/block/dm-1/slaves/dm-0/slaves/sdb1/dev
         * or /sys/block/dm-1/slaves/sdb1/dev
         */
        npath = string_printf("%s/block/%s/slaves/*{,/slaves/*}/dev", sys, part);

        glob(npath, GLOB_DOOFFS | GLOB_BRACE, NULL, &glo);

        if (glo.gl_pathc < 1) {
                globfree(&glo);
                return NULL;
        }

        dpath = cbm_dev_file_to_devpath(glo.gl_pathv[0]);
        globfree(&glo);
        if (!dpath) {
                return NULL;
        }

        blk_probe = cbm_blkid_new_probe_from_filename(dpath);
        if (!blk_probe) {
                LOG_ERROR("Unable to probe %s", dpath);
                return NULL;
        }
{'fraction_non_alphanumeric': 0.12527873504966552, 'fraction_numerical': 0.009730387188323536, 'mean_word_length': 2.1955958549222796, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 23, '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': '2797568', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1718, 'n_words': 435}