/*
* This file is part of open_agb_firm
* Copyright (C) 2021 derrek, profi200
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "types.h"
#include "error_codes.h"
#include "fs.h"
#include "fatfs/ff.h"
static const char *const g_fsPathTable[FS_MAX_DRIVES] = {FS_DRIVE_NAMES};
static struct
{
FATFS fsTable[FS_MAX_DRIVES];
FIL fTable[FS_MAX_FILES];
u32 fBitmap;
u32 fHandles;
DIR dTable[FS_MAX_DIRS];
u32 dBitmap;
u32 dHandles;
} g_fsState = {0};
static Result fres2Res(FRESULT fr)
{
if(fr != FR_OK) return fr + RES_FR_DISK_ERR - 1;
else return RES_OK;
}
static u32 findUnusedFileSlot(void)
{
if(g_fsState.fHandles >= FS_MAX_FILES) return (u32)-1;
u32 i = 0;
do
{
if((g_fsState.fBitmap & 1u< g_fsState.fHandles) return false;
else return true;
}
static u32 findUnusedDirSlot(void)
{
if(g_fsState.dHandles >= FS_MAX_DIRS) return (u32)-1;
u32 i = 0;
do
{
if((g_fsState.dBitmap & 1u< g_fsState.dHandles) return false;
else return true;
}
Result fMount(FsDrive drive)
{
if(drive >= FS_MAX_DRIVES) return RES_FR_INVALID_DRIVE;
return fres2Res(f_mount(&g_fsState.fsTable[drive], g_fsPathTable[drive], 1));
}
Result fUnmount(FsDrive drive)
{
if(drive >= FS_MAX_DRIVES) return RES_FR_INVALID_DRIVE;
return fres2Res(f_mount(NULL, g_fsPathTable[drive], 0));
}
Result fGetFree(FsDrive drive, u64 *const size)
{
if(drive >= FS_MAX_DRIVES) return RES_FR_INVALID_DRIVE;
DWORD freeClusters;
FATFS *fs;
Result res = fres2Res(f_getfree(g_fsPathTable[drive], &freeClusters, &fs));
if(res == RES_OK)
{
if(size) *size = (u64)(freeClusters * fs->csize) * 512u;
}
return res;
}
Result fOpen(FHandle *const hOut, const char *const path, u8 mode)
{
if(hOut == NULL) return RES_INVALID_ARG;
const u32 slot = findUnusedFileSlot();
if(slot == (u32)-1) return RES_FR_TOO_MANY_OPEN_FILES;
Result res = fres2Res(f_open(&g_fsState.fTable[slot], path, mode));
if(res == RES_OK)
{
g_fsState.fBitmap |= 1u<