miniffs/test/get_fs.h

55 lines
1.4 KiB
C

/******************************************************************************
* MiniFFS : Mini Flat File System
* This file is part of the test suite of MiniFFS
*
* This file abstract the filesystem opening, to be able to test both FILE and
* MEMORY backend.
*
* Copyright (c) 2008-2022 986-Studio. All rights reserved.
*
******************************************************************************/
#ifndef _WIN32
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#endif
static miniffs_t *get_fs(const char *filename)
{
#ifdef BUILD_PLATFORM_MEMORY
char *fs_image;
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);
fs_image = (char *)calloc(1, fileSize);
fread(fs_image, 1, fileSize, fp);
fclose(fp);
#else
int fd;
struct stat FileStat;
fd = open(filename, O_RDWR);
fstat(fd, &FileStat);
fs_image = (char *)mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
fileSize = FileStat.st_size;
if (fs_image == MAP_FAILED)
{
fs_image = NULL;
}
#endif
return miniffs_openfs((uintptr_t)fs_image);
#else
return miniffs_openfs(filename);
#endif
}