<commit_before>#include <stdlib.h>
#include <string.h>
#include "fsdirectory.h"
#include "apricosfsman.h"
#include "diskio.h"
#include "allocator.h"
#include "filesystem.h"

long long createDirectory(Filesystem* fs) {
    long long directoryBlock = findNextFreeBlock(fs, 0);

    if(directoryBlock != -1 && createDirectoryAtBlock(fs, directoryBlock)) {
        return directoryBlock;
    }

    return -1;
}

int createDirectoryAtBlock(Filesystem* fs, unsigned int blockNum) {
    char* dirMetadataSector;
    if(!allocateBlock(fs, blockNum))
        return 0;

    dirMetadataSector = getBlockData(fs->diskData, blockNum);
    memset(dirMetadataSector, '\0', SECTOR_SIZE);

    return 1;
}

int pushDirectoryToStack(Filesystem* fs, FsDirectory dir) {
    if(fs->currentPathUnit < MAX_PATH_DEPTH) {
        fs->pathStack[fs->currentPathUnit] = dir;
        fs->currentPathUnit++;

        return 1;
    }

    return -1;
}

FsDirectory popDirectoryFromStack(Filesystem* fs) {
    FsDirectory directory;

    if(fs->currentPathUnit <= 0) {
        directory.name = NULL;
        directory.rawData = NULL;
        directory.dirEntries = NULL;
    } else {
        directory = fs->pathStack[fs->currentPathUnit--];
    }

    return directory;
}

int openBlockAsDirectory(Filesystem* fs, unsigned int blockNum, char* dirName, FsDirectory* dir) {
    if(!dir) {
        return 0;
    }

    if(!isBlockFree(fs, blockNum)) {
        char* dirMetadataSector = getBlockData(fs->diskData, blockNum);

        dir->name = dirName;
        dir->block = blockNum;
        dir->rawData = dirMetadataSector;
        dir->dirEntries = (FsDirectoryEntry*) dirMetadataSector;

        return 1;
    }

    return 0;
}

int getFsDirectoryFromEntry(Filesystem* fs, FsDirectoryEntry* entry, FsDirectory* dir) {

    if(!fs || !entry || entry->markerAndTrackNum & DIR_ENTRY_TYPE_MASK) {
        return 0;
    } else {
        unsigned int track = entry->markerAndTrackNum & TRACK_ID_MASK;
        unsigned int sector = entry->sectorNum & SECTOR_ID_MASK;
        unsigned int block = TRACK_AND_SECTOR_TO_BLOCK(track, sector);

        return openBlockAsDirectory(fs, block, entry->name, dir);
    }
}

int isDirEntryFree(FsDirectoryEntry* entry) {
    return !(entry->markerAndTrackNum & VALID_DIR_ENTRY_MASK);
}

int findNextFreeDirEntry(FsDirectory* dir) {
    int i = 0;
    for( ; i < MAX_DIR_ENTRIES; i++) {
        if(isDirEntryFree(&dir->dirEntries[i])) {<commit_msg>Don't allow null directory pointers<commit_after>#include <stdlib.h>
#include <string.h>
#include "fsdirectory.h"
#include "apricosfsman.h"
#include "diskio.h"
#include "allocator.h"
#include "filesystem.h"

long long createDirectory(Filesystem* fs) {
    long long directoryBlock = findNextFreeBlock(fs, 0);

    if(directoryBlock != -1 && createDirectoryAtBlock(fs, directoryBlock)) {
        return directoryBlock;
    }

    return -1;
}

int createDirectoryAtBlock(Filesystem* fs, unsigned int blockNum) {
    char* dirMetadataSector;
    if(!allocateBlock(fs, blockNum))
        return 0;

    dirMetadataSector = getBlockData(fs->diskData, blockNum);
    memset(dirMetadataSector, '\0', SECTOR_SIZE);

    return 1;
}

int pushDirectoryToStack(Filesystem* fs, FsDirectory dir) {
    if(fs->currentPathUnit < MAX_PATH_DEPTH) {
        fs->pathStack[fs->currentPathUnit] = dir;
        fs->currentPathUnit++;

        return 1;
    }

    return -1;
}

FsDirectory popDirectoryFromStack(Filesystem* fs) {
    FsDirectory directory;

    if(fs->currentPathUnit <= 0) {
        directory.name = NULL;
        directory.rawData = NULL;
        directory.dirEntries = NULL;
    } else {
        directory = fs->pathStack[fs->currentPathUnit--];
    }

    return directory;
}

int openBlockAsDirectory(Filesystem* fs, unsigned int blockNum, char* dirName, FsDirectory* dir) {
    if(!dir) {
        return 0;
    }

    if(!isBlockFree(fs, blockNum)) {
        char* dirMetadataSector = getBlockData(fs->diskData, blockNum);

        dir->name = dirName;
        dir->block = blockNum;
        dir->rawData = dirMetadataSector;
        dir->dirEntries = (FsDirectoryEntry*) dirMetadataSector;

        return 1;
    }

    return 0;
}

int getFsDirectoryFromEntry(Filesystem* fs, FsDirectoryEntry* entry, FsDirectory* dir) {
    if(!dir) {
        return 0;
    }

    if(!fs || !entry || entry->markerAndTrackNum & DIR_ENTRY_TYPE_MASK) {
        return 0;
    } else {
        unsigned int track = entry->markerAndTrackNum & TRACK_ID_MASK;
        unsigned int sector = entry->sectorNum & SECTOR_ID_MASK;
        unsigned int block = TRACK_AND_SECTOR_TO_BLOCK(track, sector);

        return openBlockAsDirectory(fs, block, entry->name, dir);
    }
}

int isDirEntryFree(FsDirectoryEntry* entry) {
    return !(entry->markerAndTrackNum & VALID_DIR_ENTRY_MASK);
}

int findNextFreeDirEntry(FsDirectory* dir) {
    int i = 0;
    for( ; i < MAX_DIR_ENTRIES; i++) {
        if(isDirEntryFree(&dir->dirEntries[i])) {
{'fraction_non_alphanumeric': 0.11777733038051137, 'fraction_numerical': 0.005838534326555265, 'mean_word_length': 3.2534246575342465, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 45, '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': '2801631', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1578, 'n_words': 339}