file-io-libfat.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <wikilib.h>
  19. #include <msg.h>
  20. #include <file-io.h>
  21. #include <tff.h>
  22. #include <wl-time.h>
  23. /* FIXME: define proper error codes */
  24. /* FIXME: solve this with memory management */
  25. #define MAX_FILES (__FD_SETSIZE)
  26. static FIL fil_list[MAX_FILES];
  27. static int fil_used[MAX_FILES] = { 0 };
  28. static unsigned int fil_offset[MAX_FILES] = { 0 };
  29. int wl_open(const char *filename, int flags)
  30. {
  31. FIL *fil;
  32. int i, ret, ff_flags = 0;
  33. switch (flags) {
  34. case WL_O_RDONLY:
  35. ff_flags = FA_READ;
  36. break;
  37. case WL_O_WRONLY:
  38. #if !_FS_READONLY
  39. ff_flags = FA_WRITE;
  40. #endif
  41. break;
  42. case WL_O_RDWR:
  43. ff_flags = FA_READ;
  44. #if !_FS_READONLY
  45. ff_flags |= FA_WRITE;
  46. #endif
  47. break;
  48. case WL_O_CREATE:
  49. ff_flags = 0;
  50. #if !_FS_READONLY
  51. ff_flags = FA_WRITE | FA_CREATE_ALWAYS;
  52. #endif
  53. break;
  54. }
  55. for (i = 0; i < MAX_FILES; i++)
  56. if (!fil_used[i])
  57. break;
  58. if (i == MAX_FILES)
  59. return -1;
  60. fil = fil_list + i;
  61. ret = f_open(fil, filename, ff_flags);
  62. if (ret != 0)
  63. return -ret;
  64. fil_used[i] = 1;
  65. fil_offset[i] = 0;
  66. return i;
  67. }
  68. void wl_close(int fd)
  69. {
  70. if (fd < 0 || fd >= MAX_FILES || !fil_used[fd])
  71. return;
  72. f_close(fil_list + fd);
  73. fil_used[fd] = 0;
  74. }
  75. int wl_read(int fd, void *buf, unsigned int count)
  76. {
  77. int ret, rcount = -1;
  78. if (fd < 0 || fd >= MAX_FILES || !fil_used[fd])
  79. return -1;
  80. ret = f_read(fil_list + fd, buf, count, &rcount);
  81. if (ret)
  82. return -ret;
  83. fil_offset[fd] += rcount;
  84. return rcount;
  85. }
  86. #if _FS_READONLY
  87. int wl_write(int fd, void *buf, unsigned int count)
  88. {
  89. return -1;
  90. }
  91. #else
  92. int wl_write(int fd, void *buf, unsigned int count)
  93. {
  94. int ret, wcount;
  95. if (fd < 0 || fd >= MAX_FILES || !fil_used[fd])
  96. return -1;
  97. ret = f_write(fil_list + fd, buf, count, &wcount);
  98. if (ret)
  99. return -ret;
  100. fil_offset[fd] += wcount;
  101. if (wcount != count)
  102. return -1;
  103. return 0;
  104. }
  105. #endif /* _FS_READONLY */
  106. int wl_seek(int fd, unsigned int pos)
  107. {
  108. int ret;
  109. if (fd < 0 || fd >= MAX_FILES || !fil_used[fd])
  110. return -1;
  111. ret = f_lseek(fil_list + fd, pos);
  112. if (ret)
  113. return -ret;
  114. fil_offset[fd] = pos;
  115. return 0;
  116. }
  117. int wl_fsize(int fd, unsigned int *size)
  118. {
  119. if (fd < 0 || fd >= MAX_FILES || !fil_used[fd])
  120. return -1;
  121. *size = fil_list[fd].fsize;
  122. return 0;
  123. }
  124. unsigned int wl_tell(int fd)
  125. {
  126. return fil_offset[fd];
  127. }