Skip to content

Commit

Permalink
Prevent any file system access when not mounted
Browse files Browse the repository at this point in the history
  • Loading branch information
d0k3 committed Jan 15, 2016
1 parent bd1b00e commit fa630f9
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions source/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "fatfs/ff.h"

static bool fs_ok = false;
static FATFS fs;
static FIL file;
static DIR dir;
Expand All @@ -22,7 +23,7 @@ bool InitFS()
*(u32*)0x10000020 = 0;
*(u32*)0x10000020 = 0x340;
#endif
bool ret = (f_mount(&fs, "0:", 1) == FR_OK);
bool ret = fs_ok = (f_mount(&fs, "0:", 1) == FR_OK);
#ifdef WORK_DIR
if (ret)
f_chdir(WORK_DIR);
Expand All @@ -33,11 +34,14 @@ bool InitFS()

void DeinitFS()
{
fs_ok = false;
f_mount(NULL, "0:", 1);
}

bool FileOpen(const char* path)
{
if (!fs_ok)
return false;
unsigned flags = FA_READ | FA_WRITE | FA_OPEN_EXISTING;
bool ret = (f_open(&file, path, flags) == FR_OK);
f_lseek(&file, 0);
Expand All @@ -58,6 +62,8 @@ bool DebugFileOpen(const char* path)

bool FileCreate(const char* path, bool truncate)
{
if (!fs_ok)
return false;
unsigned flags = FA_READ | FA_WRITE;
flags |= truncate ? FA_CREATE_ALWAYS : FA_OPEN_ALWAYS;
bool ret = (f_open(&file, path, flags) == FR_OK);
Expand Down Expand Up @@ -126,6 +132,8 @@ void FileClose()

bool DirMake(const char* path)
{
if (!fs_ok)
return false;
FRESULT res = f_mkdir(path);
bool ret = (res == FR_OK) || (res == FR_EXIST);
return ret;
Expand All @@ -144,6 +152,8 @@ bool DebugDirMake(const char* path)

bool DirOpen(const char* path)
{
if (!fs_ok)
return false;
return (f_opendir(&dir, path) == FR_OK);
}

Expand Down Expand Up @@ -216,6 +226,8 @@ bool GetFileListWorker(char** list, int* lsize, char* fpath, int fsize, bool rec

bool GetFileList(const char* path, char* list, int lsize, bool recursive)
{
if (!fs_ok)
return false;
char fpath[256];
strncpy(fpath, path, 256);
return GetFileListWorker(&list, &lsize, fpath, 256, recursive);
Expand Down Expand Up @@ -244,16 +256,20 @@ static uint64_t ClustersToBytes(FATFS* fs, DWORD clusters)

uint64_t RemainingStorageSpace()
{
if (!fs_ok)
return -1;
DWORD free_clusters;
FATFS *fs2;
FRESULT res = f_getfree("0:", &free_clusters, &fs2);
if (res)
if (res != FR_OK)
return -1;

return ClustersToBytes(&fs, free_clusters);
}

uint64_t TotalStorageSpace()
{
if (!fs_ok)
return -1;
return ClustersToBytes(&fs, fs.n_fatent - 2);
}

0 comments on commit fa630f9

Please sign in to comment.