<commit_before>//
//  src/parse_dsc_for_main.c
//  tbd
//
//  Created by inoahdev on 12/01/18.
//  Copyright © 2018 - 2019 inoahdev. All rights reserved.
//

#include <errno.h>

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "handle_dsc_parse_result.h"
#include "parse_dsc_for_main.h"

#include "macho_file.h"
#include "path.h"

#include "recursive.h"
#include "unused.h"

struct image_error {
    const char *path;
    enum dsc_image_parse_result result;
};

struct dsc_iterate_images_callback_info {
    struct dyld_shared_cache_info *dsc_info;

    const char *dsc_path;
    char *write_path;

    struct tbd_for_main *global;
    struct tbd_for_main *tbd;

    struct array images;
    struct array errors;

    uint64_t write_path_length;
    uint64_t *retained_info;

    bool print_paths;
    bool parse_all_images;
};

enum dyld_cache_image_info_pad {
    E_DYLD_CACHE_IMAGE_INFO_PAD_ALREADY_EXTRACTED = 1 << 0
};

static void
clear_create_info(struct tbd_create_info *const info_in,
                  const struct tbd_create_info *const orig)
{
    tbd_create_info_destroy(info_in);
    *info_in = *orig;
}

static void
add_image_error(struct dsc_iterate_images_callback_info *const callback_info,
                const char *const image_path,
                const enum dsc_image_parse_result result)
{
    struct image_error error = {
        .path = image_path,
        .result = result
    };

    const enum array_result add_image_error_result =
        array_add_item(&callback_info->errors, sizeof(error), &error, NULL);

    if (add_image_error_result != E_ARRAY_OK) {
        if (callback_info->print_paths) {
            fprintf(stderr,
                    "Warning: Failed to append to list of errors encountered "
                    "while parsing images for dyld_shared_cache (at path %s)\n",
                    callback_info->dsc_path);
        } else {
            fputs("Warning: Failed to append to list of errors encountered "
                  "while parsing images for the provided dyld_shared_cache\n",
                  stderr);
        }
    }
}

static int
actually_parse_image(
    struct tbd_for_main *const tbd,
    struct dyld_cache_image_info *const image,
    const char *const image_path,
    struct dsc_iterate_images_callback_info *const callback_info)
{
    struct tbd_create_info *const create_info = &callback_info->tbd->info;
    const struct tbd_create_info original_info = *create_info;<commit_msg>Print more specific errors when failing to add to list of dsc errors<commit_after>//
//  src/parse_dsc_for_main.c
//  tbd
//
//  Created by inoahdev on 12/01/18.
//  Copyright © 2018 - 2019 inoahdev. All rights reserved.
//

#include <errno.h>

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "handle_dsc_parse_result.h"
#include "parse_dsc_for_main.h"

#include "macho_file.h"
#include "path.h"

#include "recursive.h"
#include "unused.h"

struct image_error {
    const char *path;
    enum dsc_image_parse_result result;
};

struct dsc_iterate_images_callback_info {
    struct dyld_shared_cache_info *dsc_info;

    const char *dsc_path;
    char *write_path;

    struct tbd_for_main *global;
    struct tbd_for_main *tbd;

    struct array images;
    struct array errors;

    uint64_t write_path_length;
    uint64_t *retained_info;

    bool print_paths;
    bool parse_all_images;
};

enum dyld_cache_image_info_pad {
    E_DYLD_CACHE_IMAGE_INFO_PAD_ALREADY_EXTRACTED = 1 << 0
};

static void
clear_create_info(struct tbd_create_info *const info_in,
                  const struct tbd_create_info *const orig)
{
    tbd_create_info_destroy(info_in);
    *info_in = *orig;
}

static void
add_image_error(struct dsc_iterate_images_callback_info *const callback_info,
                const char *const image_path,
                const enum dsc_image_parse_result result)
{
    const struct image_error error = {
        .path = image_path,
        .result = result
    };

    const enum array_result add_image_error_result =
        array_add_item(&callback_info->errors, sizeof(error), &error, NULL);

    if (add_image_error_result != E_ARRAY_OK) {
        if (callback_info->print_paths) {
            fprintf(stderr,
                    "Warning: Failed to append to list of errors encountered "
                    "while parsing image (at path %s) for dyld_shared_cache "
                    "file (at path %s)\n",
                    image_path,
                    callback_info->dsc_path);
        } else {
            fprintf(stderr,
                    "Warning: Failed to append to list of errors encountered "
                    "while parsing image (at path %s) for the provided "
                    "dyld_shared_cache file\n",
                    callback_info->dsc_path);
        }
    }
}

static int
actually_parse_image(
    struct tbd_for_main *const tbd,
    struct dyld_cache_image_info *const image,
    const char *const image_path,
    struct dsc_iterate_images_callback_info *const callback_info)
{
    struct tbd_create_info *const create_info = &callback_info->tbd->info;
    const struct tbd_create_info original_info = *create_info;
{'fraction_non_alphanumeric': 0.08130081300813008, 'fraction_numerical': 0.007742934572202865, 'mean_word_length': 2.8302446256486284, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 22, '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': '1326432', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1672, 'n_words': 408}