miniffs/platform/file.c
Godzil b31439bcb6 Now we can generate a fairly basic FS.
Added some test material
2023-02-10 17:59:15 +00:00

65 lines
1.6 KiB
C

/******************************************************************************
* MiniFFS : Mini Flat File System
* file.c: This is the file based implementation of the MiniFFS
*
* Copyright (c) 2008-2022 986-Studio. All rights reserved.
*
******************************************************************************/
#ifdef _WIN32
#include <stdio.h>
#include <stdlib.h>
#else
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#endif
#define __miniffs_internal
#include <miniffs.h>
size_t host_map_file(char *filename, char **dest)
{
char *ret_ptr;
size_t fileSize;
#ifdef _WIN32
/* As windows do not provide an easy to use mmap equivalent, let's use the fallback
* of opening the file, allocating memory and read the file in the said memory
*/
FILE *fp;
fp = fopen(filename, "rb");
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
ret_ptr = (char *)calloc(1, fileSize);
fread(ret_ptr, 1, fileSize, fp);
fclose(fp);
#else
int fd;
struct stat FileStat;
fd = open(filename, O_RDWR);
fstat(fd, &FileStat);
ret_ptr = (char *)mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
fileSize = FileStat.st_size
if (ret_ptr == MAP_FAILED)
{
ret_ptr = NULL;
}
#endif
*dest = ret_ptr;
return fileSize;
}
void host_unmap_file(char **dest, size_t length)
{
#ifdef _WIN32
/* As for windows we don't mmap, let's just free! */
free(*dest);
#else
munmap(*dest, length);
#endif
*dest = NULL;
}