<commit_before>//
//  src/mach-o/file.cc
//  tbd
//
//  Created by inoahdev on 4/24/17.
//  Copyright © 2017 inoahdev. All rights reserved.
//

#include <cerrno>

#include <fcntl.h>
#include <unistd.h>

#include "file.h"

namespace macho {
    file::open_result file::open(file *file, const char *path) noexcept {
        auto descriptor = ::open(path, O_RDONLY);
        if (descriptor == -1) {
            return open_result::failed_to_open_stream;
        }

        auto magic = macho::magic();
        if (read(descriptor, &magic, sizeof(magic)) != 1) {
            return open_result::stream_read_error;
        }

        if (!magic_is_valid(magic)) {
            return open_result::not_a_macho;
        }

        file->magic = magic;
        file->stream = fdopen(descriptor, "r");

        auto &containers = file->containers;
        auto &stream = file->stream;

        const auto magic_is_fat = macho::magic_is_fat(magic);
        if (magic_is_fat) {
            auto nfat_arch = uint32_t();
            if (fread(&nfat_arch, sizeof(nfat_arch), 1, file->stream) != 1) {
                return open_result::stream_read_error;
            }

            if (!nfat_arch) {
                return open_result::zero_architectures;
            }<commit_msg>Close descriptor on failure in `macho::file::is_valid_file()`<commit_after>//
//  src/mach-o/file.cc
//  tbd
//
//  Created by inoahdev on 4/24/17.
//  Copyright © 2017 inoahdev. All rights reserved.
//

#include <cerrno>

#include <fcntl.h>
#include <unistd.h>

#include "file.h"

namespace macho {
    file::open_result file::open(file *file, const char *path) noexcept {
        auto descriptor = ::open(path, O_RDONLY);
        if (descriptor == -1) {
            return open_result::failed_to_open_stream;
        }

        auto magic = macho::magic();
        if (read(descriptor, &magic, sizeof(magic)) != 1) {
            close(descriptor);
            return open_result::stream_read_error;
        }

        if (!magic_is_valid(magic)) {
            close(descriptor);
            return open_result::not_a_macho;
        }

        file->magic = magic;
        file->stream = fdopen(descriptor, "r");

        auto &containers = file->containers;
        auto &stream = file->stream;

        const auto magic_is_fat = macho::magic_is_fat(magic);
        if (magic_is_fat) {
            auto nfat_arch = uint32_t();
            if (fread(&nfat_arch, sizeof(nfat_arch), 1, file->stream) != 1) {
                return open_result::stream_read_error;
            }

            if (!nfat_arch) {
                return open_result::zero_architectures;
            }
{'fraction_non_alphanumeric': 0.13020439061317185, 'fraction_numerical': 0.011355034065102196, 'mean_word_length': 2.131516587677725, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '1157657', 'n_tokens_mistral': 911, 'n_tokens_neox': 841, 'n_words': 181}