file.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /******************************************************************************
  2. * MiniFFS : Mini Flat File System
  3. * file.c: This is the file based implementation of the MiniFFS
  4. *
  5. * Copyright (c) 2008-2022 986-Studio. All rights reserved.
  6. *
  7. ******************************************************************************/
  8. #ifdef _WIN32
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #else
  12. #include <sys/mman.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #endif
  17. #define __miniffs_internal
  18. #include <miniffs.h>
  19. /* Exported API */
  20. miniffs_t *miniffs_openfs(char *host_file)
  21. {
  22. return NULL;
  23. }
  24. /* Some internal functions */
  25. void *miniffs_getfileaddr(miniffs_t *fs, fileentry_t *fent)
  26. {
  27. }
  28. size_t host_map_file(char *filename, char **dest)
  29. {
  30. char *ret_ptr;
  31. size_t fileSize;
  32. #ifdef _WIN32
  33. /* As windows do not provide an easy to use mmap equivalent, let's use the fallback
  34. * of opening the file, allocating memory and read the file in the said memory
  35. */
  36. FILE *fp;
  37. fp = fopen(filename, "rb");
  38. fseek(fp, 0, SEEK_END);
  39. fileSize = ftell(fp);
  40. fseek(fp, 0, SEEK_SET);
  41. ret_ptr = (char *)calloc(1, fileSize);
  42. fread(ret_ptr, 1, fileSize, fp);
  43. fclose(fp);
  44. #else
  45. int fd;
  46. struct stat FileStat;
  47. fd = open(filename, O_RDWR);
  48. fstat(fd, &FileStat);
  49. ret_ptr = (char *)mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  50. close(fd);
  51. fileSize = FileStat.st_size
  52. if (ret_ptr == MAP_FAILED)
  53. {
  54. ret_ptr = NULL;
  55. }
  56. #endif
  57. *dest = ret_ptr;
  58. return fileSize;
  59. }
  60. void host_unmap_file(char **dest, size_t length)
  61. {
  62. #ifdef _WIN32
  63. /* As for windows we don't mmap, let's just free! */
  64. free(*dest);
  65. #else
  66. munmap(*dest, length);
  67. #endif
  68. *dest = NULL;
  69. }