mp_flipper_fileio.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <mp_flipper_fileio.h>
  4. #include <mp_flipper_runtime.h>
  5. #include "mp_flipper_context.h"
  6. #include "mp_flipper_file_helper.h"
  7. uint8_t MP_FLIPPER_FILE_ACCESS_MODE_READ = FSAM_READ;
  8. uint8_t MP_FLIPPER_FILE_ACCESS_MODE_WRITE = FSAM_WRITE;
  9. uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_EXIST = FSOM_OPEN_EXISTING;
  10. uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_ALWAYS = FSOM_OPEN_ALWAYS;
  11. uint8_t MP_FLIPPER_FILE_OPEN_MODE_OPEN_APPEND = FSOM_OPEN_APPEND;
  12. uint8_t MP_FLIPPER_FILE_OPEN_MODE_CREATE_NEW = FSOM_CREATE_NEW;
  13. uint8_t MP_FLIPPER_FILE_OPEN_MODE_CREATE_ALWAYS = FSOM_CREATE_ALWAYS;
  14. inline void* mp_flipper_file_open(const char* name, uint8_t access_mode, uint8_t open_mode) {
  15. mp_flipper_context_t* ctx = mp_flipper_context;
  16. File* file = storage_file_alloc(ctx->storage);
  17. FuriString* path = furi_string_alloc_set_str(name);
  18. do {
  19. if(mp_flipper_try_resolve_filesystem_path(path) == MP_FLIPPER_IMPORT_STAT_NO_EXIST) {
  20. mp_flipper_raise_os_error_with_filename(MP_ENOENT, name);
  21. break;
  22. }
  23. if(!storage_file_open(file, furi_string_get_cstr(path), access_mode, open_mode)) {
  24. mp_flipper_raise_os_error_with_filename(MP_ENOENT, name);
  25. break;
  26. }
  27. } while(false);
  28. // TODO close open files upon application exit
  29. return file;
  30. }
  31. inline bool mp_flipper_file_close(void* handle) {
  32. mp_flipper_context_t* ctx = mp_flipper_context;
  33. File* file = handle;
  34. if(storage_file_is_open(file) && storage_file_close(file)) {
  35. // NOP
  36. } else {
  37. // TODO handle error
  38. }
  39. storage_file_free(file);
  40. return true;
  41. }
  42. inline size_t mp_flipper_file_seek(void* handle, uint32_t offset) {
  43. return storage_file_seek(handle, offset, true);
  44. }
  45. inline size_t mp_flipper_file_tell(void* handle) {
  46. return storage_file_tell(handle);
  47. }
  48. inline size_t mp_flipper_file_size(void* handle) {
  49. return storage_file_size(handle);
  50. }
  51. inline bool mp_flipper_file_sync(void* handle) {
  52. return storage_file_sync(handle);
  53. }
  54. inline bool mp_flipper_file_eof(void* handle) {
  55. return storage_file_eof(handle);
  56. }
  57. inline size_t mp_flipper_file_read(void* handle, void* buffer, size_t size, int* errcode) {
  58. File* file = handle;
  59. *errcode = 0; // TODO handle error
  60. return storage_file_read(file, buffer, size);
  61. }
  62. inline size_t mp_flipper_file_write(void* handle, const void* buffer, size_t size, int* errcode) {
  63. File* file = handle;
  64. *errcode = 0; // TODO handle error
  65. return storage_file_write(file, buffer, size);
  66. }