<commit_before>/*
 * Copyright 2014 LKC Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "config.h"
#include "cfgprint.h"
#include "fwfile.h"
#include "util.h"

#include <stdlib.h>
#include <string.h>
#include <archive_entry.h>
#include <sodium.h>

int fwfile_add_meta_conf(cfg_t *cfg, struct archive *a, const unsigned char *signing_key)
{
    char *configtxt;
    size_t configtxt_len;

    configtxt_len = fwup_cfg_to_string(cfg, &configtxt);
    if (configtxt_len == 0)
        ERR_RETURN("Could not create meta.conf contents");

    int rc = fwfile_add_meta_conf_str(configtxt, configtxt_len, a, signing_key);

    free(configtxt);
    return rc;
}

int fwfile_add_meta_conf_str(const char *configtxt, int configtxt_len,
                             struct archive *a, const unsigned char *signing_key)
{
    struct archive_entry *entry;

    // If the user passed in a signing key, sign the meta.conf.
    if (signing_key) {
        unsigned char signature[crypto_sign_BYTES];
        crypto_sign_detached(signature, NULL,
                             (unsigned char *) configtxt, configtxt_len,
                             signing_key);

        entry = archive_entry_new();
        archive_entry_set_pathname(entry, "meta.conf.ed25519");
        archive_entry_set_size(entry, sizeof(signature));
        archive_entry_set_filetype(entry, AE_IFREG);
        archive_entry_set_perm(entry, 0644);
        archive_write_header(a, entry);
        archive_write_data(a, signature, sizeof(signature));
        archive_entry_free(entry);
    }

    // Add meta.conf
    entry = archive_entry_new();
    archive_entry_set_pathname(entry, "meta.conf");
    archive_entry_set_size(entry, configtxt_len);
    archive_entry_set_filetype(entry, AE_IFREG);
    archive_entry_set_perm(entry, 0644);
    archive_write_header(a, entry);
    archive_write_data(a, configtxt, configtxt_len);
    archive_entry_free(entry);

    return 0;
}

static int calculate_total_filesize(const char *local_paths, off_t *len)
{
    int rc = 0;
    char *paths = strdup(local_paths);

    *len = 0;
    for (char *path = strtok(paths, ";");
         path != NULL;
         path = strtok(NULL, ";")) {
        FILE *fp = fopen(path, "rb");
        if (!fp)
            ERR_CLEANUP_MSG("can't open '%s'", path);

        fseeko(fp, 0, SEEK_END);
        off_t file_len = ftello(fp);
        fseeko(fp, 0, SEEK_SET);
        fclose(fp);

        *len += file_len;
    }

cleanup:
    free(paths);
    return rc;
}

int fwfile_add_local_file(struct archive *a,
                          const char *resource_name,
                          const char *local_paths,
                          const struct fwfile_assertions *assertions)
{
    int rc = 0;

    off_t copy_buffer_len = 64 * 1024;
    char *copy_buffer = (char *) malloc(copy_buffer_len);
    struct archive_entry *entry = archive_entry_new();
    off_t total_read = 0;
    char *paths = strdup(local_paths);
    FILE *fp = NULL;

    if (*paths == '\0')
        ERR_CLEANUP_MSG("must specify a host-path for resource '%s'", resource_name);

    off_t total_len;
    if (calculate_total_filesize(local_paths, &total_len) < 0)
        goto cleanup; // Error set by calculate_total_filesize()

    if (assertions) {
        if (assertions->assert_gte >= 0 &&
                !(total_len >= assertions->assert_gte))
            ERR_CLEANUP_MSG("file size assertion failed on '%s'. Size must be >= %d bytes (%d blocks)",
                            local_paths, assertions->assert_gte, assertions->assert_gte / 512);
        if (assertions->assert_lte >= 0 &&
                !(total_len <= assertions->assert_lte))
            ERR_CLEANUP_MSG("file size assertion failed on '%s'. Size must be <= %d bytes (%d blocks)",
                            local_paths, assertions->assert_lte, assertions->assert_lte / 512);
    }

    // Convert the resource name to an archive path (most resources should be in the data directory)
    char archive_path[FWFILE_MAX_ARCHIVE_PATH];
    size_t resource_name_len = strlen(resource_name);
    if (resource_name_len + 6 > sizeof(archive_path))
        ERR_CLEANUP_MSG("resource name is too long");
    if (resource_name_len == '\0')
        ERR_CLEANUP_MSG("resource name can't be empty");
    if (resource_name[resource_name_len - 1] == '/')
        ERR_CLEANUP_MSG("resource name can't end in a '/'");<commit_msg>Improve file size assertions<commit_after>/*
 * Copyright 2014 LKC Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "config.h"
#include "cfgprint.h"
#include "fwfile.h"
#include "util.h"

#include <stdlib.h>
#include <string.h>
#include <archive_entry.h>
#include <sodium.h>

int fwfile_add_meta_conf(cfg_t *cfg, struct archive *a, const unsigned char *signing_key)
{
    char *configtxt;
    size_t configtxt_len;

    configtxt_len = fwup_cfg_to_string(cfg, &configtxt);
    if (configtxt_len == 0)
        ERR_RETURN("Could not create meta.conf contents");

    int rc = fwfile_add_meta_conf_str(configtxt, configtxt_len, a, signing_key);

    free(configtxt);
    return rc;
}

int fwfile_add_meta_conf_str(const char *configtxt, int configtxt_len,
                             struct archive *a, const unsigned char *signing_key)
{
    struct archive_entry *entry;

    // If the user passed in a signing key, sign the meta.conf.
    if (signing_key) {
        unsigned char signature[crypto_sign_BYTES];
        crypto_sign_detached(signature, NULL,
                             (unsigned char *) configtxt, configtxt_len,
                             signing_key);

        entry = archive_entry_new();
        archive_entry_set_pathname(entry, "meta.conf.ed25519");
        archive_entry_set_size(entry, sizeof(signature));
        archive_entry_set_filetype(entry, AE_IFREG);
        archive_entry_set_perm(entry, 0644);
        archive_write_header(a, entry);
        archive_write_data(a, signature, sizeof(signature));
        archive_entry_free(entry);
    }

    // Add meta.conf
    entry = archive_entry_new();
    archive_entry_set_pathname(entry, "meta.conf");
    archive_entry_set_size(entry, configtxt_len);
    archive_entry_set_filetype(entry, AE_IFREG);
    archive_entry_set_perm(entry, 0644);
    archive_write_header(a, entry);
    archive_write_data(a, configtxt, configtxt_len);
    archive_entry_free(entry);

    return 0;
}

static int calculate_total_filesize(const char *local_paths, off_t *len)
{
    int rc = 0;
    char *paths = strdup(local_paths);

    *len = 0;
    for (char *path = strtok(paths, ";");
         path != NULL;
         path = strtok(NULL, ";")) {
        FILE *fp = fopen(path, "rb");
        if (!fp)
            ERR_CLEANUP_MSG("can't open '%s'", path);

        fseeko(fp, 0, SEEK_END);
        off_t file_len = ftello(fp);
        fseeko(fp, 0, SEEK_SET);
        fclose(fp);

        *len += file_len;
    }

cleanup:
    free(paths);
    return rc;
}

int fwfile_add_local_file(struct archive *a,
                          const char *resource_name,
                          const char *local_paths,
                          const struct fwfile_assertions *assertions)
{
    int rc = 0;

    off_t copy_buffer_len = 64 * 1024;
    char *copy_buffer = (char *) malloc(copy_buffer_len);
    struct archive_entry *entry = archive_entry_new();
    off_t total_read = 0;
    char *paths = strdup(local_paths);
    FILE *fp = NULL;

    if (*paths == '\0')
        ERR_CLEANUP_MSG("must specify a host-path for resource '%s'", resource_name);

    off_t total_len;
    if (calculate_total_filesize(local_paths, &total_len) < 0)
        goto cleanup; // Error set by calculate_total_filesize()

    if (assertions) {
        if (assertions->assert_gte >= 0 &&
                !(total_len >= assertions->assert_gte))
            ERR_CLEANUP_MSG("file size assertion failed on '%s'. Size is %d bytes. It must be >= %d bytes (%d blocks)",
                            local_paths, total_len, assertions->assert_gte, assertions->assert_gte / 512);
        if (assertions->assert_lte >= 0 &&
                !(total_len <= assertions->assert_lte))
            ERR_CLEANUP_MSG("file size assertion failed on '%s'. Size is %d bytes. It must be <= %d bytes (%d blocks)",
                            local_paths, total_len, assertions->assert_lte, assertions->assert_lte / 512);
    }

    // Convert the resource name to an archive path (most resources should be in the data directory)
    char archive_path[FWFILE_MAX_ARCHIVE_PATH];
    size_t resource_name_len = strlen(resource_name);
    if (resource_name_len + 6 > sizeof(archive_path))
        ERR_CLEANUP_MSG("resource name '%s' is too long", resource_name);
    if (resource_name_len == '\0')
        ERR_CLEANUP_MSG("resource name can't be empty");
    if (resource_name[resource_name_len - 1] == '/')
        ERR_CLEANUP_MSG("resource name '%s' can't end in a '/'", resource_name);
{'fraction_non_alphanumeric': 0.10587644399799095, 'fraction_numerical': 0.009643395278754395, 'mean_word_length': 2.873929961089494, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5644244', 'n_tokens_mistral': 3208, 'n_tokens_neox': 3068, 'n_words': 841}