storage_glue.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "storage_glue.h"
  2. #include <furi_hal.h>
  3. /****************** storage file ******************/
  4. void storage_file_init(StorageFile* obj) {
  5. obj->file = NULL;
  6. obj->file_data = NULL;
  7. obj->path = furi_string_alloc();
  8. }
  9. void storage_file_init_set(StorageFile* obj, const StorageFile* src) {
  10. obj->file = src->file;
  11. obj->file_data = src->file_data;
  12. obj->path = furi_string_alloc_set(src->path);
  13. }
  14. void storage_file_set(StorageFile* obj, const StorageFile* src) { //-V524
  15. obj->file = src->file;
  16. obj->file_data = src->file_data;
  17. furi_string_set(obj->path, src->path);
  18. }
  19. void storage_file_clear(StorageFile* obj) {
  20. furi_string_free(obj->path);
  21. }
  22. /****************** storage data ******************/
  23. void storage_data_init(StorageData* storage) {
  24. storage->data = NULL;
  25. storage->status = StorageStatusNotReady;
  26. StorageFileList_init(storage->files);
  27. }
  28. StorageStatus storage_data_status(StorageData* storage) {
  29. return storage->status;
  30. }
  31. const char* storage_data_status_text(StorageData* storage) {
  32. const char* result = "unknown";
  33. switch(storage->status) {
  34. case StorageStatusOK:
  35. result = "ok";
  36. break;
  37. case StorageStatusNotReady:
  38. result = "not ready";
  39. break;
  40. case StorageStatusNotMounted:
  41. result = "not mounted";
  42. break;
  43. case StorageStatusNoFS:
  44. result = "no filesystem";
  45. break;
  46. case StorageStatusNotAccessible:
  47. result = "not accessible";
  48. break;
  49. case StorageStatusErrorInternal:
  50. result = "internal";
  51. break;
  52. }
  53. return result;
  54. }
  55. void storage_data_timestamp(StorageData* storage) {
  56. storage->timestamp = furi_hal_rtc_get_timestamp();
  57. }
  58. uint32_t storage_data_get_timestamp(StorageData* storage) {
  59. return storage->timestamp;
  60. }
  61. /****************** storage glue ******************/
  62. static StorageFile* storage_get_file(const File* file, StorageData* storage) {
  63. StorageFile* storage_file_ref = NULL;
  64. StorageFileList_it_t it;
  65. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  66. StorageFileList_next(it)) {
  67. StorageFile* storage_file = StorageFileList_ref(it);
  68. if(storage_file->file->file_id == file->file_id) {
  69. storage_file_ref = storage_file;
  70. break;
  71. }
  72. }
  73. return storage_file_ref;
  74. }
  75. bool storage_has_file(const File* file, StorageData* storage) {
  76. return storage_get_file(file, storage) != NULL;
  77. }
  78. bool storage_path_already_open(FuriString* path, StorageData* storage) {
  79. bool open = false;
  80. StorageFileList_it_t it;
  81. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  82. StorageFileList_next(it)) {
  83. const StorageFile* storage_file = StorageFileList_cref(it);
  84. if(furi_string_cmp(storage_file->path, path) == 0) {
  85. open = true;
  86. break;
  87. }
  88. }
  89. return open;
  90. }
  91. void storage_set_storage_file_data(const File* file, void* file_data, StorageData* storage) {
  92. StorageFile* storage_file_ref = storage_get_file(file, storage);
  93. furi_check(storage_file_ref != NULL);
  94. storage_file_ref->file_data = file_data;
  95. }
  96. void* storage_get_storage_file_data(const File* file, StorageData* storage) {
  97. StorageFile* storage_file_ref = storage_get_file(file, storage);
  98. furi_check(storage_file_ref != NULL);
  99. return storage_file_ref->file_data;
  100. }
  101. void storage_push_storage_file(File* file, FuriString* path, StorageData* storage) {
  102. StorageFile* storage_file = StorageFileList_push_new(storage->files);
  103. file->file_id = (uint32_t)storage_file;
  104. storage_file->file = file;
  105. furi_string_set(storage_file->path, path);
  106. }
  107. bool storage_pop_storage_file(File* file, StorageData* storage) {
  108. StorageFileList_it_t it;
  109. bool result = false;
  110. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  111. StorageFileList_next(it)) {
  112. if(StorageFileList_cref(it)->file->file_id == file->file_id) {
  113. result = true;
  114. break;
  115. }
  116. }
  117. if(result) {
  118. StorageFileList_remove(storage->files, it);
  119. }
  120. return result;
  121. }