2023-02-19 20:47:31 +01:00
|
|
|
#include <stdio.h>
|
2023-02-27 12:57:14 +01:00
|
|
|
#include <string.h>
|
2023-02-19 20:47:31 +01:00
|
|
|
|
|
|
|
#include "fileops.h"
|
2024-02-13 03:19:57 +01:00
|
|
|
#include "malloc.h"
|
2023-02-19 20:47:31 +01:00
|
|
|
|
2024-03-02 00:07:58 +01:00
|
|
|
static struct stat st;
|
|
|
|
|
2023-02-19 20:47:31 +01:00
|
|
|
bool FSOPFileExists(const char* file)
|
|
|
|
{
|
2024-03-02 00:07:58 +01:00
|
|
|
return !stat(file, &st) && !S_ISDIR(st.st_mode);
|
2023-02-19 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FSOPFolderExists(const char* path)
|
|
|
|
{
|
2024-03-02 00:07:58 +01:00
|
|
|
return !stat(path, &st) && S_ISDIR(st.st_mode);
|
2023-02-19 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t FSOPGetFileSizeBytes(const char* path)
|
|
|
|
{
|
2024-03-02 00:07:58 +01:00
|
|
|
if (stat(path, &st) < 0) return 0;
|
2023-02-19 20:47:31 +01:00
|
|
|
|
2024-03-02 00:07:58 +01:00
|
|
|
return st.st_size;
|
2023-02-19 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FSOPDeleteFile(const char* file)
|
|
|
|
{
|
2024-03-02 00:07:58 +01:00
|
|
|
if (FSOPFileExists(file))
|
|
|
|
remove(file);
|
2023-02-19 20:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FSOPMakeFolder(const char* path)
|
|
|
|
{
|
|
|
|
if (FSOPFolderExists(path))
|
|
|
|
return;
|
|
|
|
|
2023-02-27 12:57:14 +01:00
|
|
|
char* pos = strchr(path, '/');
|
|
|
|
s32 current = pos - path;
|
|
|
|
current++;
|
|
|
|
pos = strchr(path + current, '/');
|
|
|
|
|
|
|
|
while (pos)
|
|
|
|
{
|
|
|
|
*pos = 0;
|
|
|
|
mkdir(path, S_IREAD | S_IWRITE);
|
|
|
|
*pos = '/';
|
|
|
|
|
|
|
|
current = pos - path;
|
|
|
|
current++;
|
|
|
|
pos = strchr(path + current, '/');
|
|
|
|
}
|
|
|
|
|
2023-02-19 20:47:31 +01:00
|
|
|
mkdir(path, S_IREAD | S_IWRITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 FSOPReadOpenFile(FILE* fp, void* buffer, u32 offset, u32 length)
|
|
|
|
{
|
|
|
|
fseek(fp, offset, SEEK_SET);
|
|
|
|
return fread(buffer, length, 1, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 FSOPReadOpenFileA(FILE* fp, void** buffer, u32 offset, u32 length)
|
|
|
|
{
|
2024-02-13 03:19:57 +01:00
|
|
|
*buffer = memalign32(length);
|
2023-02-19 20:47:31 +01:00
|
|
|
if (!*buffer)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
s32 ret = FSOPReadOpenFile(fp, *buffer, offset, length);
|
2024-03-02 00:07:58 +01:00
|
|
|
if (ret <= 0)
|
|
|
|
{
|
2023-02-19 20:47:31 +01:00
|
|
|
free(*buffer);
|
2024-03-02 00:07:58 +01:00
|
|
|
*buffer = NULL;
|
|
|
|
}
|
2023-02-19 20:47:31 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|