storage_glue.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. bool storage_has_file(const File* file, StorageData* storage_data) {
  63. bool result = false;
  64. StorageFileList_it_t it;
  65. for(StorageFileList_it(it, storage_data->files); !StorageFileList_end_p(it);
  66. StorageFileList_next(it)) {
  67. const StorageFile* storage_file = StorageFileList_cref(it);
  68. if(storage_file->file->file_id == file->file_id) {
  69. result = true;
  70. break;
  71. }
  72. }
  73. return result;
  74. }
  75. bool storage_path_already_open(FuriString* path, StorageFileList_t array) {
  76. bool open = false;
  77. StorageFileList_it_t it;
  78. for(StorageFileList_it(it, array); !StorageFileList_end_p(it); StorageFileList_next(it)) {
  79. const StorageFile* storage_file = StorageFileList_cref(it);
  80. if(furi_string_cmp(storage_file->path, path) == 0) {
  81. open = true;
  82. break;
  83. }
  84. }
  85. return open;
  86. }
  87. void storage_set_storage_file_data(const File* file, void* file_data, StorageData* storage) {
  88. StorageFile* founded_file = NULL;
  89. StorageFileList_it_t it;
  90. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  91. StorageFileList_next(it)) {
  92. StorageFile* storage_file = StorageFileList_ref(it);
  93. if(storage_file->file->file_id == file->file_id) {
  94. founded_file = storage_file;
  95. break;
  96. }
  97. }
  98. furi_check(founded_file != NULL);
  99. founded_file->file_data = file_data;
  100. }
  101. void* storage_get_storage_file_data(const File* file, StorageData* storage) {
  102. const StorageFile* founded_file = NULL;
  103. StorageFileList_it_t it;
  104. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  105. StorageFileList_next(it)) {
  106. const StorageFile* storage_file = StorageFileList_cref(it);
  107. if(storage_file->file->file_id == file->file_id) {
  108. founded_file = storage_file;
  109. break;
  110. }
  111. }
  112. furi_check(founded_file != NULL);
  113. return founded_file->file_data;
  114. }
  115. void storage_push_storage_file(File* file, FuriString* path, StorageData* storage) {
  116. StorageFile* storage_file = StorageFileList_push_new(storage->files);
  117. file->file_id = (uint32_t)storage_file;
  118. storage_file->file = file;
  119. furi_string_set(storage_file->path, path);
  120. }
  121. bool storage_pop_storage_file(File* file, StorageData* storage) {
  122. StorageFileList_it_t it;
  123. bool result = false;
  124. for(StorageFileList_it(it, storage->files); !StorageFileList_end_p(it);
  125. StorageFileList_next(it)) {
  126. if(StorageFileList_cref(it)->file->file_id == file->file_id) {
  127. result = true;
  128. break;
  129. }
  130. }
  131. if(result) {
  132. StorageFileList_remove(storage->files, it);
  133. }
  134. return result;
  135. }