storage_test_app.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <storage/storage.h>
  4. #define TAG "StorageTest"
  5. #define BYTES_COUNT 16
  6. #define TEST_STRING "TestDataStringProvidedByDiceRoll"
  7. #define SEEK_OFFSET_FROM_START 10
  8. #define SEEK_OFFSET_INCREASE 12
  9. #define SEEK_OFFSET_SUM (SEEK_OFFSET_FROM_START + SEEK_OFFSET_INCREASE)
  10. static void do_file_test(Storage* api, const char* path) {
  11. File* file = storage_file_alloc(api);
  12. bool result;
  13. uint8_t bytes[BYTES_COUNT + 1];
  14. uint8_t bytes_count;
  15. uint64_t position;
  16. uint64_t size;
  17. FURI_LOG_I(TAG, "--------- FILE \"%s\" ---------", path);
  18. // open
  19. result = storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  20. if(result) {
  21. FURI_LOG_I(TAG, "open");
  22. } else {
  23. FURI_LOG_E(TAG, "open, %s", storage_file_get_error_desc(file));
  24. }
  25. // write
  26. bytes_count = storage_file_write(file, TEST_STRING, strlen(TEST_STRING));
  27. if(bytes_count == 0) {
  28. FURI_LOG_E(TAG, "write, %s", storage_file_get_error_desc(file));
  29. } else {
  30. FURI_LOG_I(TAG, "write");
  31. }
  32. // sync
  33. result = storage_file_sync(file);
  34. if(result) {
  35. FURI_LOG_I(TAG, "sync");
  36. } else {
  37. FURI_LOG_E(TAG, "sync, %s", storage_file_get_error_desc(file));
  38. }
  39. // eof #1
  40. result = storage_file_eof(file);
  41. if(result) {
  42. FURI_LOG_I(TAG, "eof #1");
  43. } else {
  44. FURI_LOG_E(TAG, "eof #1, %s", storage_file_get_error_desc(file));
  45. }
  46. // seek from start and tell
  47. result = storage_file_seek(file, SEEK_OFFSET_FROM_START, true);
  48. if(result) {
  49. FURI_LOG_I(TAG, "seek #1");
  50. } else {
  51. FURI_LOG_E(TAG, "seek #1, %s", storage_file_get_error_desc(file));
  52. }
  53. position = storage_file_tell(file);
  54. if(position != SEEK_OFFSET_FROM_START) {
  55. FURI_LOG_E(TAG, "tell #1, %s", storage_file_get_error_desc(file));
  56. } else {
  57. FURI_LOG_I(TAG, "tell #1");
  58. }
  59. // size
  60. size = storage_file_size(file);
  61. if(size != strlen(TEST_STRING)) {
  62. FURI_LOG_E(TAG, "size #1, %s", storage_file_get_error_desc(file));
  63. } else {
  64. FURI_LOG_I(TAG, "size #1");
  65. }
  66. // seek and tell
  67. result = storage_file_seek(file, SEEK_OFFSET_INCREASE, false);
  68. if(result) {
  69. FURI_LOG_I(TAG, "seek #2");
  70. } else {
  71. FURI_LOG_E(TAG, "seek #2, %s", storage_file_get_error_desc(file));
  72. }
  73. position = storage_file_tell(file);
  74. if(position != SEEK_OFFSET_SUM) {
  75. FURI_LOG_E(TAG, "tell #2, %s", storage_file_get_error_desc(file));
  76. } else {
  77. FURI_LOG_I(TAG, "tell #2");
  78. }
  79. // eof #2
  80. result = storage_file_eof(file);
  81. if(!result) {
  82. FURI_LOG_I(TAG, "eof #2");
  83. } else {
  84. FURI_LOG_E(TAG, "eof #2, %s", storage_file_get_error_desc(file));
  85. }
  86. // truncate
  87. result = storage_file_truncate(file);
  88. if(result) {
  89. FURI_LOG_I(TAG, "truncate");
  90. } else {
  91. FURI_LOG_E(TAG, "truncate, %s", storage_file_get_error_desc(file));
  92. }
  93. size = storage_file_size(file);
  94. if(size != SEEK_OFFSET_SUM) {
  95. FURI_LOG_E(TAG, "size #2, %s", storage_file_get_error_desc(file));
  96. } else {
  97. FURI_LOG_I(TAG, "size #2");
  98. }
  99. // close
  100. result = storage_file_close(file);
  101. if(result) {
  102. FURI_LOG_I(TAG, "close");
  103. } else {
  104. FURI_LOG_E(TAG, "close, error");
  105. }
  106. // open
  107. result = storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING);
  108. if(result) {
  109. FURI_LOG_I(TAG, "open");
  110. } else {
  111. FURI_LOG_E(TAG, "open, %s", storage_file_get_error_desc(file));
  112. }
  113. // read
  114. memset(bytes, 0, BYTES_COUNT + 1);
  115. bytes_count = storage_file_read(file, bytes, BYTES_COUNT);
  116. if(bytes_count == 0) {
  117. FURI_LOG_E(TAG, "read, %s", storage_file_get_error_desc(file));
  118. } else {
  119. if(memcmp(TEST_STRING, bytes, bytes_count) == 0) {
  120. FURI_LOG_I(TAG, "read");
  121. } else {
  122. FURI_LOG_E(TAG, "read, garbage");
  123. }
  124. }
  125. // close
  126. result = storage_file_close(file);
  127. if(result) {
  128. FURI_LOG_I(TAG, "close");
  129. } else {
  130. FURI_LOG_E(TAG, "close, error");
  131. }
  132. storage_file_free(file);
  133. }
  134. static void do_dir_test(Storage* api, const char* path) {
  135. File* file = storage_file_alloc(api);
  136. bool result;
  137. FURI_LOG_I(TAG, "--------- DIR \"%s\" ---------", path);
  138. // open
  139. result = storage_dir_open(file, path);
  140. if(result) {
  141. FURI_LOG_I(TAG, "open");
  142. } else {
  143. FURI_LOG_E(TAG, "open, %s", storage_file_get_error_desc(file));
  144. }
  145. // read
  146. const uint8_t filename_size = 100;
  147. char* filename = malloc(filename_size);
  148. FileInfo fileinfo;
  149. do {
  150. result = storage_dir_read(file, &fileinfo, filename, filename_size);
  151. if(result) {
  152. if(strlen(filename)) {
  153. FURI_LOG_I(
  154. TAG,
  155. "read #1, [%s]%s",
  156. ((fileinfo.flags & FSF_DIRECTORY) ? "D" : "F"),
  157. filename);
  158. }
  159. } else if(storage_file_get_error(file) != FSE_NOT_EXIST) {
  160. FURI_LOG_E(TAG, "read #1, %s", storage_file_get_error_desc(file));
  161. break;
  162. }
  163. } while(result);
  164. // rewind
  165. result = storage_dir_rewind(file);
  166. if(result) {
  167. FURI_LOG_I(TAG, "rewind");
  168. } else {
  169. FURI_LOG_E(TAG, "rewind, %s", storage_file_get_error_desc(file));
  170. }
  171. // read
  172. do {
  173. result = storage_dir_read(file, &fileinfo, filename, filename_size);
  174. if(result) {
  175. if(strlen(filename)) {
  176. FURI_LOG_I(
  177. TAG,
  178. "read #2, [%s]%s",
  179. ((fileinfo.flags & FSF_DIRECTORY) ? "D" : "F"),
  180. filename);
  181. }
  182. } else if(storage_file_get_error(file) != FSE_NOT_EXIST) {
  183. FURI_LOG_E(TAG, "read #2, %s", storage_file_get_error_desc(file));
  184. break;
  185. }
  186. } while((strlen(filename)));
  187. // close
  188. result = storage_dir_close(file);
  189. if(result) {
  190. FURI_LOG_I(TAG, "close");
  191. } else {
  192. FURI_LOG_E(TAG, "close, error");
  193. }
  194. storage_file_free(file);
  195. free(filename);
  196. }
  197. static void do_test_start(Storage* api, const char* path) {
  198. string_t str_path;
  199. string_init_printf(str_path, "%s/test-folder", path);
  200. FURI_LOG_I(TAG, "--------- START \"%s\" ---------", path);
  201. // mkdir
  202. FS_Error result = storage_common_mkdir(api, string_get_cstr(str_path));
  203. if(result == FSE_OK) {
  204. FURI_LOG_I(TAG, "mkdir ok");
  205. } else {
  206. FURI_LOG_E(TAG, "mkdir, %s", storage_error_get_desc(result));
  207. }
  208. // stat
  209. FileInfo fileinfo;
  210. result = storage_common_stat(api, string_get_cstr(str_path), &fileinfo);
  211. if(result == FSE_OK) {
  212. if(fileinfo.flags & FSF_DIRECTORY) {
  213. FURI_LOG_I(TAG, "stat #1 ok");
  214. } else {
  215. FURI_LOG_E(TAG, "stat #1, %s", storage_error_get_desc(result));
  216. }
  217. } else {
  218. FURI_LOG_E(TAG, "stat #1, %s", storage_error_get_desc(result));
  219. }
  220. string_clear(str_path);
  221. }
  222. static void do_test_end(Storage* api, const char* path) {
  223. uint64_t total_space;
  224. uint64_t free_space;
  225. string_t str_path_1;
  226. string_t str_path_2;
  227. string_init_printf(str_path_1, "%s/test-folder", path);
  228. string_init_printf(str_path_2, "%s/test-folder2", path);
  229. FURI_LOG_I(TAG, "--------- END \"%s\" ---------", path);
  230. // fs stat
  231. FS_Error result = storage_common_fs_info(api, path, &total_space, &free_space);
  232. if(result == FSE_OK) {
  233. uint32_t total_kb = total_space / 1024;
  234. uint32_t free_kb = free_space / 1024;
  235. FURI_LOG_I(TAG, "fs_info: total %luk, free %luk", total_kb, free_kb);
  236. } else {
  237. FURI_LOG_E(TAG, "fs_info, %s", storage_error_get_desc(result));
  238. }
  239. // rename #1
  240. result = storage_common_rename(api, string_get_cstr(str_path_1), string_get_cstr(str_path_2));
  241. if(result == FSE_OK) {
  242. FURI_LOG_I(TAG, "rename #1 ok");
  243. } else {
  244. FURI_LOG_E(TAG, "rename #1, %s", storage_error_get_desc(result));
  245. }
  246. // remove #1
  247. result = storage_common_remove(api, string_get_cstr(str_path_2));
  248. if(result == FSE_OK) {
  249. FURI_LOG_I(TAG, "remove #1 ok");
  250. } else {
  251. FURI_LOG_E(TAG, "remove #1, %s", storage_error_get_desc(result));
  252. }
  253. // rename #2
  254. string_printf(str_path_1, "%s/test.txt", path);
  255. string_printf(str_path_2, "%s/test2.txt", path);
  256. result = storage_common_rename(api, string_get_cstr(str_path_1), string_get_cstr(str_path_2));
  257. if(result == FSE_OK) {
  258. FURI_LOG_I(TAG, "rename #2 ok");
  259. } else {
  260. FURI_LOG_E(TAG, "rename #2, %s", storage_error_get_desc(result));
  261. }
  262. // remove #2
  263. result = storage_common_remove(api, string_get_cstr(str_path_2));
  264. if(result == FSE_OK) {
  265. FURI_LOG_I(TAG, "remove #2 ok");
  266. } else {
  267. FURI_LOG_E(TAG, "remove #2, %s", storage_error_get_desc(result));
  268. }
  269. string_clear(str_path_1);
  270. string_clear(str_path_2);
  271. }
  272. int32_t storage_test_app(void* p) {
  273. UNUSED(p);
  274. Storage* api = furi_record_open("storage");
  275. do_test_start(api, "/int");
  276. do_test_start(api, "/any");
  277. do_test_start(api, "/ext");
  278. do_file_test(api, "/int/test.txt");
  279. do_file_test(api, "/any/test.txt");
  280. do_file_test(api, "/ext/test.txt");
  281. do_dir_test(api, "/int");
  282. do_dir_test(api, "/any");
  283. do_dir_test(api, "/ext");
  284. do_test_end(api, "/int");
  285. do_test_end(api, "/any");
  286. do_test_end(api, "/ext");
  287. while(true) {
  288. furi_hal_delay_ms(1000);
  289. }
  290. return 0;
  291. }