mp_flipper_fileio.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. inline void* mp_flipper_file_open(
  8. const char* name,
  9. mp_flipper_file_access_mode_t access_mode,
  10. mp_flipper_file_open_mode_t open_mode,
  11. size_t* offset) {
  12. mp_flipper_context_t* ctx = mp_flipper_context;
  13. File* file = storage_file_alloc(ctx->storage);
  14. FuriString* path = furi_string_alloc_set_str(name);
  15. do {
  16. if(mp_flipper_try_resolve_filesystem_path(path) == MP_FLIPPER_IMPORT_STAT_NO_EXIST) {
  17. mp_flipper_raise_os_error_with_filename(MP_ENOENT, name);
  18. break;
  19. }
  20. if(!storage_file_open(file, furi_string_get_cstr(path), access_mode, open_mode)) {
  21. mp_flipper_raise_os_error_with_filename(MP_ENOENT, name);
  22. break;
  23. }
  24. } while(false);
  25. // TODO close open files upon application exit
  26. return file;
  27. }
  28. inline int mp_flipper_file_close(void* handle) {
  29. mp_flipper_context_t* ctx = mp_flipper_context;
  30. File* file = handle;
  31. if(storage_file_is_open(file) && storage_file_close(file)) {
  32. // NOP
  33. } else {
  34. // TODO handle error
  35. }
  36. storage_file_free(file);
  37. return 0;
  38. }
  39. inline bool mp_flipper_file_writable(void* handle) {
  40. File* file = handle;
  41. // TODO
  42. return true;
  43. }
  44. inline size_t mp_flipper_file_read(void* handle, void* buffer, size_t size, int* errcode) {
  45. File* file = handle;
  46. *errcode = 0; // TODO handle error
  47. return storage_file_read(file, buffer, size);
  48. }
  49. inline size_t mp_flipper_file_write(void* handle, const void* buffer, size_t size, int* errcode) {
  50. File* file = handle;
  51. *errcode = 0; // TODO handle error
  52. return storage_file_write(file, buffer, size);
  53. }