vfs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "vfs.h"
  2. /* dirent that will be given to callers;
  3. * note: both APIs assume that only one dirent ever exists
  4. */
  5. vfs_dirent_t dir_ent;
  6. FIL guard_for_the_whole_fs;
  7. int vfs_read(void *buffer, int dummy, int len, vfs_file_t *file)
  8. {
  9. unsigned int bytesread;
  10. (void)dummy; /* suppress unused warning */
  11. FRESULT r = f_read(file, buffer, len, &bytesread);
  12. if (r != FR_OK)
  13. return 0;
  14. return bytesread;
  15. }
  16. vfs_dirent_t *vfs_readdir(vfs_dir_t *dir)
  17. {
  18. FILINFO fi;
  19. #if _USE_LFN
  20. fi.lfname = NULL;
  21. #endif
  22. FRESULT r = f_readdir(dir, &fi);
  23. if (r != FR_OK)
  24. return NULL;
  25. if (fi.fname[0] == 0)
  26. return NULL;
  27. memcpy(dir_ent.name, fi.fname, sizeof(fi.fname));
  28. return &dir_ent;
  29. }
  30. int vfs_stat(vfs_t *vfs, const char *filename, vfs_stat_t *st)
  31. {
  32. FILINFO f;
  33. #if _USE_LFN
  34. f.lfname = NULL;
  35. #endif
  36. (void)vfs; /* suppress unused warning */
  37. if (FR_OK != f_stat(filename, &f))
  38. {
  39. return 1;
  40. }
  41. st->st_size = f.fsize;
  42. st->st_mode = f.fattrib;
  43. struct tm tm = {
  44. .tm_sec = 2 * (f.ftime & 0x1f),
  45. .tm_min = (f.ftime >> 5) & 0x3f,
  46. .tm_hour = (f.ftime >> 11) & 0x1f,
  47. .tm_mday = f.fdate & 0x1f,
  48. .tm_mon = (f.fdate >> 5) & 0xf,
  49. .tm_year = 80 + ((f.fdate >> 9) & 0x7f),
  50. };
  51. st->st_mtime = mktime(&tm);
  52. return 0;
  53. }
  54. void vfs_close(vfs_t *vfs)
  55. {
  56. if (vfs != &guard_for_the_whole_fs)
  57. {
  58. /* Close a file */
  59. f_close(vfs);
  60. free(vfs);
  61. }
  62. }
  63. int vfs_write(void *buffer, int dummy, int len, vfs_file_t *file)
  64. {
  65. unsigned int byteswritten;
  66. (void)dummy; /* suppress unused warning */
  67. FRESULT r = f_write(file, buffer, len, &byteswritten);
  68. if (r != FR_OK)
  69. return 0;
  70. return byteswritten;
  71. }
  72. vfs_t *vfs_openfs()
  73. {
  74. return &guard_for_the_whole_fs;
  75. }
  76. vfs_file_t *vfs_open(vfs_t *vfs, const char *filename, const char *mode)
  77. {
  78. vfs_file_t *f = malloc(sizeof(vfs_file_t));
  79. BYTE flags = 0;
  80. (void)vfs; /* suppress unused warning */
  81. while (*mode != '\0')
  82. {
  83. if (*mode == 'r')
  84. flags |= FA_READ;
  85. if (*mode == 'w')
  86. flags |= FA_WRITE | FA_CREATE_ALWAYS;
  87. mode++;
  88. }
  89. FRESULT r = f_open(f, filename, flags);
  90. if (FR_OK != r)
  91. {
  92. free(f);
  93. return NULL;
  94. }
  95. return f;
  96. }
  97. char *vfs_getcwd(vfs_t *vfs, void *dummy1, int dummy2)
  98. {
  99. char *cwd = malloc(255);
  100. FRESULT r = f_getcwd(cwd, 255);
  101. (void)dummy1; /* suppress unused warning */
  102. (void)dummy2;
  103. (void)vfs;
  104. if (r != FR_OK)
  105. {
  106. free(cwd);
  107. return NULL;
  108. }
  109. return cwd;
  110. }
  111. vfs_dir_t *vfs_opendir(vfs_t *vfs, const char *path)
  112. {
  113. vfs_dir_t *dir = malloc(sizeof *dir);
  114. FRESULT r = f_opendir(dir, path);
  115. (void)vfs; /* suppress unused warning */
  116. if (FR_OK != r)
  117. {
  118. free(dir);
  119. return NULL;
  120. }
  121. return dir;
  122. }
  123. void vfs_closedir(vfs_dir_t *dir)
  124. {
  125. if (dir)
  126. {
  127. f_closedir(dir);
  128. free(dir);
  129. }
  130. }
  131. struct tm dummy = {
  132. .tm_year = 70,
  133. .tm_mon = 0,
  134. .tm_mday = 1,
  135. .tm_hour = 0,
  136. .tm_min = 0};
  137. struct tm *gmtime(const time_t *c_t)
  138. {
  139. (void)c_t;
  140. return &dummy;
  141. }