get_fs.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /******************************************************************************
  2. * MiniFFS : Mini Flat File System
  3. * This file is part of the test suite of MiniFFS
  4. *
  5. * This file abstract the filesystem opening, to be able to test both FILE and
  6. * MEMORY backend.
  7. *
  8. * Copyright (c) 2008-2022 986-Studio. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. static miniffs_t *get_fs(const char *filename)
  12. {
  13. #ifdef BUILD_PLATFORM_MEMORY
  14. char *fs_image;
  15. size_t fileSize;
  16. #ifdef _WIN32
  17. /* As windows do not provide an easy to use mmap equivalent, let's use the fallback
  18. * of opening the file, allocating memory and read the file in the said memory
  19. */
  20. FILE *fp;
  21. fp = fopen(filename, "rb");
  22. fseek(fp, 0, SEEK_END);
  23. fileSize = ftell(fp);
  24. fseek(fp, 0, SEEK_SET);
  25. fs_image = (char *)calloc(1, fileSize);
  26. fread(fs_image, 1, fileSize, fp);
  27. fclose(fp);
  28. #else
  29. int fd;
  30. struct stat FileStat;
  31. fd = open(filename, O_RDWR);
  32. fstat(fd, &FileStat);
  33. fs_image = (char *)mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  34. close(fd);
  35. fileSize = FileStat.st_size
  36. if (fs_image == MAP_FAILED)
  37. {
  38. fs_image = NULL;
  39. }
  40. #endif
  41. return miniffs_openfs((uintptr_t)fs_image);
  42. #else
  43. return miniffs_openfs(filename);
  44. #endif
  45. }