test_fatfs_common.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <fcntl.h>
  19. #include <sys/time.h>
  20. #include <sys/unistd.h>
  21. #include <sys/stat.h>
  22. #include <errno.h>
  23. #include <utime.h>
  24. #include "unity.h"
  25. #include "esp_log.h"
  26. #include "esp_system.h"
  27. #include "esp_vfs.h"
  28. #include "esp_vfs_fat.h"
  29. #include "freertos/FreeRTOS.h"
  30. #include "freertos/task.h"
  31. #include "ff.h"
  32. #include "test_fatfs_common.h"
  33. #include "esp_rom_sys.h"
  34. const char* fatfs_test_hello_str = "Hello, World!\n";
  35. const char* fatfs_test_hello_str_utf = "世界,你好!\n";
  36. void test_fatfs_create_file_with_text(const char* name, const char* text)
  37. {
  38. FILE* f = fopen(name, "wb");
  39. TEST_ASSERT_NOT_NULL(f);
  40. TEST_ASSERT_TRUE(fputs(text, f) != EOF);
  41. TEST_ASSERT_EQUAL(0, fclose(f));
  42. }
  43. void test_fatfs_overwrite_append(const char* filename)
  44. {
  45. /* Create new file with 'aaaa' */
  46. test_fatfs_create_file_with_text(filename, "aaaa");
  47. /* Append 'bbbb' to file */
  48. FILE *f_a = fopen(filename, "a");
  49. TEST_ASSERT_NOT_NULL(f_a);
  50. TEST_ASSERT_NOT_EQUAL(EOF, fputs("bbbb", f_a));
  51. TEST_ASSERT_EQUAL(0, fclose(f_a));
  52. /* Read back 8 bytes from file, verify it's 'aaaabbbb' */
  53. char buf[10] = { 0 };
  54. FILE *f_r = fopen(filename, "r");
  55. TEST_ASSERT_NOT_NULL(f_r);
  56. TEST_ASSERT_EQUAL(8, fread(buf, 1, 8, f_r));
  57. TEST_ASSERT_EQUAL_STRING_LEN("aaaabbbb", buf, 8);
  58. /* Be sure we're at end of file */
  59. TEST_ASSERT_EQUAL(0, fread(buf, 1, 8, f_r));
  60. TEST_ASSERT_EQUAL(0, fclose(f_r));
  61. /* Overwrite file with 'cccc' */
  62. test_fatfs_create_file_with_text(filename, "cccc");
  63. /* Verify file now only contains 'cccc' */
  64. f_r = fopen(filename, "r");
  65. TEST_ASSERT_NOT_NULL(f_r);
  66. bzero(buf, sizeof(buf));
  67. TEST_ASSERT_EQUAL(4, fread(buf, 1, 8, f_r)); // trying to read 8 bytes, only expecting 4
  68. TEST_ASSERT_EQUAL_STRING_LEN("cccc", buf, 4);
  69. TEST_ASSERT_EQUAL(0, fclose(f_r));
  70. }
  71. void test_fatfs_read_file(const char* filename)
  72. {
  73. FILE* f = fopen(filename, "r");
  74. TEST_ASSERT_NOT_NULL(f);
  75. char buf[32] = { 0 };
  76. int cb = fread(buf, 1, sizeof(buf), f);
  77. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str), cb);
  78. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str, buf));
  79. TEST_ASSERT_EQUAL(0, fclose(f));
  80. }
  81. void test_fatfs_read_file_utf_8(const char* filename)
  82. {
  83. FILE* f = fopen(filename, "r");
  84. TEST_ASSERT_NOT_NULL(f);
  85. char buf[64] = { 0 }; //Doubled buffer size to allow for longer UTF-8 strings
  86. int cb = fread(buf, 1, sizeof(buf), f);
  87. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str_utf), cb);
  88. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str_utf, buf));
  89. TEST_ASSERT_EQUAL(0, fclose(f));
  90. }
  91. void test_fatfs_pread_file(const char* filename)
  92. {
  93. char buf[32] = { 0 };
  94. const int fd = open(filename, O_RDONLY);
  95. TEST_ASSERT_NOT_EQUAL(-1, fd);
  96. int r = pread(fd, buf, sizeof(buf), 0); // it is a regular read() with offset==0
  97. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str, buf));
  98. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str), r);
  99. memset(buf, 0, sizeof(buf));
  100. r = pread(fd, buf, sizeof(buf), 1); // offset==1
  101. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str + 1, buf));
  102. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str) - 1, r);
  103. memset(buf, 0, sizeof(buf));
  104. r = pread(fd, buf, sizeof(buf), 5); // offset==5
  105. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str + 5, buf));
  106. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str) - 5, r);
  107. // regular read() should work now because pread() should not affect the current position in file
  108. memset(buf, 0, sizeof(buf));
  109. r = read(fd, buf, sizeof(buf)); // note that this is read() and not pread()
  110. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str, buf));
  111. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str), r);
  112. memset(buf, 0, sizeof(buf));
  113. r = pread(fd, buf, sizeof(buf), 10); // offset==10
  114. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str + 10, buf));
  115. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str) - 10, r);
  116. memset(buf, 0, sizeof(buf));
  117. r = pread(fd, buf, sizeof(buf), strlen(fatfs_test_hello_str) + 1); // offset to EOF
  118. TEST_ASSERT_EQUAL(0, r);
  119. TEST_ASSERT_EQUAL(0, close(fd));
  120. }
  121. static void test_pwrite(const char *filename, off_t offset, const char *msg)
  122. {
  123. const int fd = open(filename, O_WRONLY);
  124. TEST_ASSERT_NOT_EQUAL(-1, fd);
  125. const off_t current_pos = lseek(fd, 0, SEEK_END); // O_APPEND is not the same - jumps to the end only before write()
  126. const int r = pwrite(fd, msg, strlen(msg), offset);
  127. TEST_ASSERT_EQUAL(strlen(msg), r);
  128. TEST_ASSERT_EQUAL(current_pos, lseek(fd, 0, SEEK_CUR)); // pwrite should not move the pointer
  129. TEST_ASSERT_EQUAL(0, close(fd));
  130. }
  131. static void test_file_content(const char *filename, const char *msg)
  132. {
  133. char buf[32] = { 0 };
  134. const int fd = open(filename, O_RDONLY);
  135. TEST_ASSERT_NOT_EQUAL(-1, fd);
  136. int r = read(fd, buf, sizeof(buf));
  137. TEST_ASSERT_NOT_EQUAL(-1, r);
  138. TEST_ASSERT_EQUAL(0, strcmp(msg, buf));
  139. TEST_ASSERT_EQUAL(0, close(fd));
  140. }
  141. void test_fatfs_pwrite_file(const char *filename)
  142. {
  143. int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
  144. TEST_ASSERT_NOT_EQUAL(-1, fd);
  145. TEST_ASSERT_EQUAL(0, close(fd));
  146. test_pwrite(filename, 0, "Hello");
  147. test_file_content(filename, "Hello");
  148. test_pwrite(filename, strlen("Hello"), ", world!");
  149. test_file_content(filename, "Hello, world!");
  150. test_pwrite(filename, strlen("Hello, "), "Dolly");
  151. test_file_content(filename, "Hello, Dolly!");
  152. }
  153. void test_fatfs_open_max_files(const char* filename_prefix, size_t files_count)
  154. {
  155. FILE** files = calloc(files_count, sizeof(FILE*));
  156. for (size_t i = 0; i < files_count; ++i) {
  157. char name[32];
  158. snprintf(name, sizeof(name), "%s_%d.txt", filename_prefix, i);
  159. files[i] = fopen(name, "w");
  160. TEST_ASSERT_NOT_NULL(files[i]);
  161. }
  162. /* close everything and clean up */
  163. for (size_t i = 0; i < files_count; ++i) {
  164. fclose(files[i]);
  165. }
  166. free(files);
  167. }
  168. void test_fatfs_lseek(const char* filename)
  169. {
  170. FILE* f = fopen(filename, "wb+");
  171. TEST_ASSERT_NOT_NULL(f);
  172. TEST_ASSERT_EQUAL(11, fprintf(f, "0123456789\n"));
  173. TEST_ASSERT_EQUAL(0, fseek(f, -2, SEEK_CUR));
  174. TEST_ASSERT_EQUAL('9', fgetc(f));
  175. TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_SET));
  176. TEST_ASSERT_EQUAL('3', fgetc(f));
  177. TEST_ASSERT_EQUAL(0, fseek(f, -3, SEEK_END));
  178. TEST_ASSERT_EQUAL('8', fgetc(f));
  179. TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_END));
  180. TEST_ASSERT_EQUAL(14, ftell(f));
  181. TEST_ASSERT_EQUAL(4, fprintf(f, "abc\n"));
  182. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  183. TEST_ASSERT_EQUAL(18, ftell(f));
  184. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_SET));
  185. char buf[20];
  186. TEST_ASSERT_EQUAL(18, fread(buf, 1, sizeof(buf), f));
  187. const char ref_buf[] = "0123456789\n\0\0\0abc\n";
  188. TEST_ASSERT_EQUAL_INT8_ARRAY(ref_buf, buf, sizeof(ref_buf) - 1);
  189. TEST_ASSERT_EQUAL(0, fclose(f));
  190. #ifdef CONFIG_FATFS_USE_FASTSEEK
  191. f = fopen(filename, "rb+");
  192. TEST_ASSERT_NOT_NULL(f);
  193. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  194. TEST_ASSERT_EQUAL(18, ftell(f));
  195. TEST_ASSERT_EQUAL(0, fseek(f, -4, SEEK_CUR));
  196. TEST_ASSERT_EQUAL(14, ftell(f));
  197. TEST_ASSERT_EQUAL(0, fseek(f, -14, SEEK_CUR));
  198. TEST_ASSERT_EQUAL(0, ftell(f));
  199. TEST_ASSERT_EQUAL(18, fread(buf, 1, sizeof(buf), f));
  200. TEST_ASSERT_EQUAL_INT8_ARRAY(ref_buf, buf, sizeof(ref_buf) - 1);
  201. TEST_ASSERT_EQUAL(0, fclose(f));
  202. #endif
  203. }
  204. void test_fatfs_truncate_file(const char* filename)
  205. {
  206. int read = 0;
  207. int truncated_len = 0;
  208. const char input[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  209. char output[sizeof(input)];
  210. FILE* f = fopen(filename, "wb");
  211. TEST_ASSERT_NOT_NULL(f);
  212. TEST_ASSERT_EQUAL(strlen(input), fprintf(f, input));
  213. TEST_ASSERT_EQUAL(0, fclose(f));
  214. // Extending file beyond size is not supported
  215. TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input) + 1));
  216. TEST_ASSERT_EQUAL(errno, EPERM);
  217. TEST_ASSERT_EQUAL(-1, truncate(filename, -1));
  218. TEST_ASSERT_EQUAL(errno, EINVAL);
  219. // Truncating should succeed
  220. const char truncated_1[] = "ABCDEFGHIJ";
  221. truncated_len = strlen(truncated_1);
  222. TEST_ASSERT_EQUAL(0, truncate(filename, truncated_len));
  223. f = fopen(filename, "rb");
  224. TEST_ASSERT_NOT_NULL(f);
  225. memset(output, 0, sizeof(output));
  226. read = fread(output, 1, sizeof(output), f);
  227. TEST_ASSERT_EQUAL(truncated_len, read);
  228. TEST_ASSERT_EQUAL_STRING_LEN(truncated_1, output, truncated_len);
  229. TEST_ASSERT_EQUAL(0, fclose(f));
  230. // Once truncated, the new file size should be the basis
  231. // whether truncation should succeed or not
  232. TEST_ASSERT_EQUAL(-1, truncate(filename, truncated_len + 1));
  233. TEST_ASSERT_EQUAL(EPERM, errno);
  234. TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input)));
  235. TEST_ASSERT_EQUAL(EPERM, errno);
  236. TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input) + 1));
  237. TEST_ASSERT_EQUAL(EPERM, errno);
  238. TEST_ASSERT_EQUAL(-1, truncate(filename, -1));
  239. TEST_ASSERT_EQUAL(EINVAL, errno);
  240. // Truncating a truncated file should succeed
  241. const char truncated_2[] = "ABCDE";
  242. truncated_len = strlen(truncated_2);
  243. TEST_ASSERT_EQUAL(0, truncate(filename, truncated_len));
  244. f = fopen(filename, "rb");
  245. TEST_ASSERT_NOT_NULL(f);
  246. memset(output, 0, sizeof(output));
  247. read = fread(output, 1, sizeof(output), f);
  248. TEST_ASSERT_EQUAL(truncated_len, read);
  249. TEST_ASSERT_EQUAL_STRING_LEN(truncated_2, output, truncated_len);
  250. TEST_ASSERT_EQUAL(0, fclose(f));
  251. }
  252. void test_fatfs_stat(const char* filename, const char* root_dir)
  253. {
  254. struct tm tm;
  255. tm.tm_year = 2017 - 1900;
  256. tm.tm_mon = 11;
  257. tm.tm_mday = 8;
  258. tm.tm_hour = 19;
  259. tm.tm_min = 51;
  260. tm.tm_sec = 10;
  261. time_t t = mktime(&tm);
  262. printf("Setting time: %s", asctime(&tm));
  263. struct timeval now = { .tv_sec = t };
  264. settimeofday(&now, NULL);
  265. test_fatfs_create_file_with_text(filename, "foo\n");
  266. struct stat st;
  267. TEST_ASSERT_EQUAL(0, stat(filename, &st));
  268. time_t mtime = st.st_mtime;
  269. struct tm mtm;
  270. localtime_r(&mtime, &mtm);
  271. printf("File time: %s", asctime(&mtm));
  272. TEST_ASSERT(abs(mtime - t) < 2); // fatfs library stores time with 2 second precision
  273. TEST_ASSERT(st.st_mode & S_IFREG);
  274. TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
  275. memset(&st, 0, sizeof(st));
  276. TEST_ASSERT_EQUAL(0, stat(root_dir, &st));
  277. TEST_ASSERT(st.st_mode & S_IFDIR);
  278. TEST_ASSERT_FALSE(st.st_mode & S_IFREG);
  279. }
  280. void test_fatfs_utime(const char* filename, const char* root_dir)
  281. {
  282. struct stat achieved_stat;
  283. struct tm desired_tm;
  284. struct utimbuf desired_time = {
  285. .actime = 0, // access time is not supported
  286. .modtime = 0,
  287. };
  288. time_t false_now = 0;
  289. memset(&desired_tm, 0, sizeof(struct tm));
  290. {
  291. // Setting up a false actual time - used when the file is created and for modification with the current time
  292. desired_tm.tm_mon = 10 - 1;
  293. desired_tm.tm_mday = 31;
  294. desired_tm.tm_year = 2018 - 1900;
  295. desired_tm.tm_hour = 10;
  296. desired_tm.tm_min = 35;
  297. desired_tm.tm_sec = 23;
  298. false_now = mktime(&desired_tm);
  299. struct timeval now = { .tv_sec = false_now };
  300. settimeofday(&now, NULL);
  301. }
  302. test_fatfs_create_file_with_text(filename, "");
  303. // 00:00:00. January 1st, 1980 - FATFS cannot handle earlier dates
  304. desired_tm.tm_mon = 1 - 1;
  305. desired_tm.tm_mday = 1;
  306. desired_tm.tm_year = 1980 - 1900;
  307. desired_tm.tm_hour = 0;
  308. desired_tm.tm_min = 0;
  309. desired_tm.tm_sec = 0;
  310. printf("Testing mod. time: %s", asctime(&desired_tm));
  311. desired_time.modtime = mktime(&desired_tm);
  312. TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
  313. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  314. TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
  315. // current time
  316. TEST_ASSERT_EQUAL(0, utime(filename, NULL));
  317. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  318. printf("Mod. time changed to (false actual time): %s", ctime(&achieved_stat.st_mtime));
  319. TEST_ASSERT_NOT_EQUAL(desired_time.modtime, achieved_stat.st_mtime);
  320. TEST_ASSERT(false_now - achieved_stat.st_mtime <= 2); // two seconds of tolerance are given
  321. // 23:59:08. December 31st, 2037
  322. desired_tm.tm_mon = 12 - 1;
  323. desired_tm.tm_mday = 31;
  324. desired_tm.tm_year = 2037 - 1900;
  325. desired_tm.tm_hour = 23;
  326. desired_tm.tm_min = 59;
  327. desired_tm.tm_sec = 8;
  328. printf("Testing mod. time: %s", asctime(&desired_tm));
  329. desired_time.modtime = mktime(&desired_tm);
  330. TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
  331. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  332. TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
  333. //WARNING: it has the Unix Millenium bug (Y2K38)
  334. // 00:00:00. January 1st, 1970 - FATFS cannot handle years before 1980
  335. desired_tm.tm_mon = 1 - 1;
  336. desired_tm.tm_mday = 1;
  337. desired_tm.tm_year = 1970 - 1900;
  338. desired_tm.tm_hour = 0;
  339. desired_tm.tm_min = 0;
  340. desired_tm.tm_sec = 0;
  341. printf("Testing mod. time: %s", asctime(&desired_tm));
  342. desired_time.modtime = mktime(&desired_tm);
  343. TEST_ASSERT_EQUAL(-1, utime(filename, &desired_time));
  344. TEST_ASSERT_EQUAL(EINVAL, errno);
  345. }
  346. void test_fatfs_unlink(const char* filename)
  347. {
  348. test_fatfs_create_file_with_text(filename, "unlink\n");
  349. TEST_ASSERT_EQUAL(0, unlink(filename));
  350. TEST_ASSERT_NULL(fopen(filename, "r"));
  351. }
  352. void test_fatfs_link_rename(const char* filename_prefix)
  353. {
  354. char name_copy[64];
  355. char name_dst[64];
  356. char name_src[64];
  357. snprintf(name_copy, sizeof(name_copy), "%s_cpy.txt", filename_prefix);
  358. snprintf(name_dst, sizeof(name_dst), "%s_dst.txt", filename_prefix);
  359. snprintf(name_src, sizeof(name_src), "%s_src.txt", filename_prefix);
  360. unlink(name_copy);
  361. unlink(name_dst);
  362. unlink(name_src);
  363. FILE* f = fopen(name_src, "w+");
  364. TEST_ASSERT_NOT_NULL(f);
  365. const char* str = "0123456789";
  366. for (int i = 0; i < 4000; ++i) {
  367. TEST_ASSERT_NOT_EQUAL(EOF, fputs(str, f));
  368. }
  369. TEST_ASSERT_EQUAL(0, fclose(f));
  370. TEST_ASSERT_EQUAL(0, link(name_src, name_copy));
  371. FILE* fcopy = fopen(name_copy, "r");
  372. TEST_ASSERT_NOT_NULL(fcopy);
  373. TEST_ASSERT_EQUAL(0, fseek(fcopy, 0, SEEK_END));
  374. TEST_ASSERT_EQUAL(40000, ftell(fcopy));
  375. TEST_ASSERT_EQUAL(0, fclose(fcopy));
  376. TEST_ASSERT_EQUAL(0, rename(name_copy, name_dst));
  377. TEST_ASSERT_NULL(fopen(name_copy, "r"));
  378. FILE* fdst = fopen(name_dst, "r");
  379. TEST_ASSERT_NOT_NULL(fdst);
  380. TEST_ASSERT_EQUAL(0, fseek(fdst, 0, SEEK_END));
  381. TEST_ASSERT_EQUAL(40000, ftell(fdst));
  382. TEST_ASSERT_EQUAL(0, fclose(fdst));
  383. }
  384. void test_fatfs_mkdir_rmdir(const char* filename_prefix)
  385. {
  386. char name_dir1[64];
  387. char name_dir2[64];
  388. char name_dir2_file[64];
  389. snprintf(name_dir1, sizeof(name_dir1), "%s1", filename_prefix);
  390. snprintf(name_dir2, sizeof(name_dir2), "%s2", filename_prefix);
  391. snprintf(name_dir2_file, sizeof(name_dir2_file), "%s2/1.txt", filename_prefix);
  392. TEST_ASSERT_EQUAL(0, mkdir(name_dir1, 0755));
  393. struct stat st;
  394. TEST_ASSERT_EQUAL(0, stat(name_dir1, &st));
  395. TEST_ASSERT_TRUE(st.st_mode & S_IFDIR);
  396. TEST_ASSERT_FALSE(st.st_mode & S_IFREG);
  397. TEST_ASSERT_EQUAL(0, rmdir(name_dir1));
  398. TEST_ASSERT_EQUAL(-1, stat(name_dir1, &st));
  399. TEST_ASSERT_EQUAL(0, mkdir(name_dir2, 0755));
  400. test_fatfs_create_file_with_text(name_dir2_file, "foo\n");
  401. TEST_ASSERT_EQUAL(0, stat(name_dir2, &st));
  402. TEST_ASSERT_TRUE(st.st_mode & S_IFDIR);
  403. TEST_ASSERT_FALSE(st.st_mode & S_IFREG);
  404. TEST_ASSERT_EQUAL(0, stat(name_dir2_file, &st));
  405. TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
  406. TEST_ASSERT_TRUE(st.st_mode & S_IFREG);
  407. TEST_ASSERT_EQUAL(-1, rmdir(name_dir2));
  408. TEST_ASSERT_EQUAL(0, unlink(name_dir2_file));
  409. TEST_ASSERT_EQUAL(0, rmdir(name_dir2));
  410. }
  411. void test_fatfs_can_opendir(const char* path)
  412. {
  413. char name_dir_file[64];
  414. const char * file_name = "test_opd.txt";
  415. snprintf(name_dir_file, sizeof(name_dir_file), "%s/%s", path, file_name);
  416. unlink(name_dir_file);
  417. test_fatfs_create_file_with_text(name_dir_file, "test_opendir\n");
  418. DIR* dir = opendir(path);
  419. TEST_ASSERT_NOT_NULL(dir);
  420. bool found = false;
  421. while (true) {
  422. struct dirent* de = readdir(dir);
  423. if (!de) {
  424. break;
  425. }
  426. if (strcasecmp(de->d_name, file_name) == 0) {
  427. found = true;
  428. break;
  429. }
  430. }
  431. TEST_ASSERT_TRUE(found);
  432. TEST_ASSERT_EQUAL(0, closedir(dir));
  433. unlink(name_dir_file);
  434. }
  435. void test_fatfs_opendir_readdir_rewinddir(const char* dir_prefix)
  436. {
  437. char name_dir_inner_file[64];
  438. char name_dir_inner[64];
  439. char name_dir_file3[64];
  440. char name_dir_file2[64];
  441. char name_dir_file1[64];
  442. snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/inner/3.txt", dir_prefix);
  443. snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/inner", dir_prefix);
  444. snprintf(name_dir_file3, sizeof(name_dir_file2), "%s/boo.bin", dir_prefix);
  445. snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/2.txt", dir_prefix);
  446. snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/1.txt", dir_prefix);
  447. unlink(name_dir_inner_file);
  448. rmdir(name_dir_inner);
  449. unlink(name_dir_file1);
  450. unlink(name_dir_file2);
  451. unlink(name_dir_file3);
  452. rmdir(dir_prefix);
  453. TEST_ASSERT_EQUAL(0, mkdir(dir_prefix, 0755));
  454. test_fatfs_create_file_with_text(name_dir_file1, "1\n");
  455. test_fatfs_create_file_with_text(name_dir_file2, "2\n");
  456. test_fatfs_create_file_with_text(name_dir_file3, "\01\02\03");
  457. TEST_ASSERT_EQUAL(0, mkdir(name_dir_inner, 0755));
  458. test_fatfs_create_file_with_text(name_dir_inner_file, "3\n");
  459. DIR* dir = opendir(dir_prefix);
  460. TEST_ASSERT_NOT_NULL(dir);
  461. int count = 0;
  462. const char* names[4];
  463. while(count < 4) {
  464. struct dirent* de = readdir(dir);
  465. if (!de) {
  466. break;
  467. }
  468. printf("found '%s'\n", de->d_name);
  469. if (strcasecmp(de->d_name, "1.txt") == 0) {
  470. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  471. names[count] = "1.txt";
  472. ++count;
  473. } else if (strcasecmp(de->d_name, "2.txt") == 0) {
  474. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  475. names[count] = "2.txt";
  476. ++count;
  477. } else if (strcasecmp(de->d_name, "inner") == 0) {
  478. TEST_ASSERT_TRUE(de->d_type == DT_DIR);
  479. names[count] = "inner";
  480. ++count;
  481. } else if (strcasecmp(de->d_name, "boo.bin") == 0) {
  482. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  483. names[count] = "boo.bin";
  484. ++count;
  485. } else {
  486. TEST_FAIL_MESSAGE("unexpected directory entry");
  487. }
  488. }
  489. TEST_ASSERT_EQUAL(count, 4);
  490. rewinddir(dir);
  491. struct dirent* de = readdir(dir);
  492. TEST_ASSERT_NOT_NULL(de);
  493. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
  494. seekdir(dir, 3);
  495. de = readdir(dir);
  496. TEST_ASSERT_NOT_NULL(de);
  497. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
  498. seekdir(dir, 1);
  499. de = readdir(dir);
  500. TEST_ASSERT_NOT_NULL(de);
  501. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
  502. seekdir(dir, 2);
  503. de = readdir(dir);
  504. TEST_ASSERT_NOT_NULL(de);
  505. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
  506. TEST_ASSERT_EQUAL(0, closedir(dir));
  507. }
  508. void test_fatfs_opendir_readdir_rewinddir_utf_8(const char* dir_prefix)
  509. {
  510. char name_dir_inner_file[64];
  511. char name_dir_inner[64];
  512. char name_dir_file3[64];
  513. char name_dir_file2[64];
  514. char name_dir_file1[64];
  515. snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/内部目录/内部文件.txt", dir_prefix);
  516. snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/内部目录", dir_prefix);
  517. snprintf(name_dir_file3, sizeof(name_dir_file3), "%s/文件三.bin", dir_prefix);
  518. snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/文件二.txt", dir_prefix);
  519. snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/文件一.txt", dir_prefix);
  520. unlink(name_dir_inner_file);
  521. rmdir(name_dir_inner);
  522. unlink(name_dir_file1);
  523. unlink(name_dir_file2);
  524. unlink(name_dir_file3);
  525. rmdir(dir_prefix);
  526. TEST_ASSERT_EQUAL(0, mkdir(dir_prefix, 0755));
  527. test_fatfs_create_file_with_text(name_dir_file1, "一号\n");
  528. test_fatfs_create_file_with_text(name_dir_file2, "二号\n");
  529. test_fatfs_create_file_with_text(name_dir_file3, "\0一\0二\0三");
  530. TEST_ASSERT_EQUAL(0, mkdir(name_dir_inner, 0755));
  531. test_fatfs_create_file_with_text(name_dir_inner_file, "三号\n");
  532. DIR* dir = opendir(dir_prefix);
  533. TEST_ASSERT_NOT_NULL(dir);
  534. int count = 0;
  535. const char* names[4];
  536. while(count < 4) {
  537. struct dirent* de = readdir(dir);
  538. if (!de) {
  539. break;
  540. }
  541. printf("found '%s'\n", de->d_name);
  542. if (strcasecmp(de->d_name, "文件一.txt") == 0) {
  543. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  544. names[count] = "文件一.txt";
  545. ++count;
  546. } else if (strcasecmp(de->d_name, "文件二.txt") == 0) {
  547. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  548. names[count] = "文件二.txt";
  549. ++count;
  550. } else if (strcasecmp(de->d_name, "内部目录") == 0) {
  551. TEST_ASSERT_TRUE(de->d_type == DT_DIR);
  552. names[count] = "内部目录";
  553. ++count;
  554. } else if (strcasecmp(de->d_name, "文件三.bin") == 0) {
  555. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  556. names[count] = "文件三.bin";
  557. ++count;
  558. } else {
  559. TEST_FAIL_MESSAGE("unexpected directory entry");
  560. }
  561. }
  562. TEST_ASSERT_EQUAL(count, 4);
  563. rewinddir(dir);
  564. struct dirent* de = readdir(dir);
  565. TEST_ASSERT_NOT_NULL(de);
  566. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
  567. seekdir(dir, 3);
  568. de = readdir(dir);
  569. TEST_ASSERT_NOT_NULL(de);
  570. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
  571. seekdir(dir, 1);
  572. de = readdir(dir);
  573. TEST_ASSERT_NOT_NULL(de);
  574. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
  575. seekdir(dir, 2);
  576. de = readdir(dir);
  577. TEST_ASSERT_NOT_NULL(de);
  578. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
  579. TEST_ASSERT_EQUAL(0, closedir(dir));
  580. }
  581. typedef struct {
  582. const char* filename;
  583. bool write;
  584. size_t word_count;
  585. int seed;
  586. SemaphoreHandle_t done;
  587. int result;
  588. } read_write_test_arg_t;
  589. #define READ_WRITE_TEST_ARG_INIT(name, seed_) \
  590. { \
  591. .filename = name, \
  592. .seed = seed_, \
  593. .word_count = 8192, \
  594. .write = true, \
  595. .done = xSemaphoreCreateBinary() \
  596. }
  597. static void read_write_task(void* param)
  598. {
  599. read_write_test_arg_t* args = (read_write_test_arg_t*) param;
  600. FILE* f = fopen(args->filename, args->write ? "wb" : "rb");
  601. if (f == NULL) {
  602. args->result = ESP_ERR_NOT_FOUND;
  603. goto done;
  604. }
  605. srand(args->seed);
  606. for (size_t i = 0; i < args->word_count; ++i) {
  607. uint32_t val = rand();
  608. if (args->write) {
  609. int cnt = fwrite(&val, sizeof(val), 1, f);
  610. if (cnt != 1) {
  611. esp_rom_printf("E(w): i=%d, cnt=%d val=%d\n\n", i, cnt, val);
  612. args->result = ESP_FAIL;
  613. goto close;
  614. }
  615. } else {
  616. uint32_t rval;
  617. int cnt = fread(&rval, sizeof(rval), 1, f);
  618. if (cnt != 1 || rval != val) {
  619. esp_rom_printf("E(r): i=%d, cnt=%d rval=%d val=%d\n\n", i, cnt, rval, val);
  620. args->result = ESP_FAIL;
  621. goto close;
  622. }
  623. }
  624. }
  625. args->result = ESP_OK;
  626. close:
  627. fclose(f);
  628. done:
  629. xSemaphoreGive(args->done);
  630. vTaskDelay(1);
  631. vTaskDelete(NULL);
  632. }
  633. void test_fatfs_concurrent(const char* filename_prefix)
  634. {
  635. char names[4][64];
  636. for (size_t i = 0; i < 4; ++i) {
  637. snprintf(names[i], sizeof(names[i]), "%s%d", filename_prefix, i + 1);
  638. unlink(names[i]);
  639. }
  640. read_write_test_arg_t args1 = READ_WRITE_TEST_ARG_INIT(names[0], 1);
  641. read_write_test_arg_t args2 = READ_WRITE_TEST_ARG_INIT(names[1], 2);
  642. printf("writing f1 and f2\n");
  643. const int cpuid_0 = 0;
  644. const int cpuid_1 = portNUM_PROCESSORS - 1;
  645. const int stack_size = 4096;
  646. xTaskCreatePinnedToCore(&read_write_task, "rw1", stack_size, &args1, 3, NULL, cpuid_0);
  647. xTaskCreatePinnedToCore(&read_write_task, "rw2", stack_size, &args2, 3, NULL, cpuid_1);
  648. xSemaphoreTake(args1.done, portMAX_DELAY);
  649. printf("f1 done\n");
  650. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  651. xSemaphoreTake(args2.done, portMAX_DELAY);
  652. printf("f2 done\n");
  653. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  654. args1.write = false;
  655. args2.write = false;
  656. read_write_test_arg_t args3 = READ_WRITE_TEST_ARG_INIT(names[2], 3);
  657. read_write_test_arg_t args4 = READ_WRITE_TEST_ARG_INIT(names[3], 4);
  658. printf("reading f1 and f2, writing f3 and f4\n");
  659. xTaskCreatePinnedToCore(&read_write_task, "rw3", stack_size, &args3, 3, NULL, cpuid_1);
  660. xTaskCreatePinnedToCore(&read_write_task, "rw4", stack_size, &args4, 3, NULL, cpuid_0);
  661. xTaskCreatePinnedToCore(&read_write_task, "rw1", stack_size, &args1, 3, NULL, cpuid_0);
  662. xTaskCreatePinnedToCore(&read_write_task, "rw2", stack_size, &args2, 3, NULL, cpuid_1);
  663. xSemaphoreTake(args1.done, portMAX_DELAY);
  664. printf("f1 done\n");
  665. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  666. xSemaphoreTake(args2.done, portMAX_DELAY);
  667. printf("f2 done\n");
  668. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  669. xSemaphoreTake(args3.done, portMAX_DELAY);
  670. printf("f3 done\n");
  671. TEST_ASSERT_EQUAL(ESP_OK, args3.result);
  672. xSemaphoreTake(args4.done, portMAX_DELAY);
  673. printf("f4 done\n");
  674. TEST_ASSERT_EQUAL(ESP_OK, args4.result);
  675. vSemaphoreDelete(args1.done);
  676. vSemaphoreDelete(args2.done);
  677. vSemaphoreDelete(args3.done);
  678. vSemaphoreDelete(args4.done);
  679. }
  680. void test_fatfs_rw_speed(const char* filename, void* buf, size_t buf_size, size_t file_size, bool is_write)
  681. {
  682. const size_t buf_count = file_size / buf_size;
  683. FILE* f = fopen(filename, (is_write) ? "wb" : "rb");
  684. TEST_ASSERT_NOT_NULL(f);
  685. struct timeval tv_start;
  686. gettimeofday(&tv_start, NULL);
  687. for (size_t n = 0; n < buf_count; ++n) {
  688. if (is_write) {
  689. TEST_ASSERT_EQUAL(buf_size, write(fileno(f), buf, buf_size));
  690. } else {
  691. if (read(fileno(f), buf, buf_size) != buf_size) {
  692. printf("reading at n=%d, eof=%d", n, feof(f));
  693. TEST_FAIL();
  694. }
  695. }
  696. }
  697. struct timeval tv_end;
  698. gettimeofday(&tv_end, NULL);
  699. TEST_ASSERT_EQUAL(0, fclose(f));
  700. float t_s = tv_end.tv_sec - tv_start.tv_sec + 1e-6f * (tv_end.tv_usec - tv_start.tv_usec);
  701. printf("%s %d bytes (block size %d) in %.3fms (%.3f MB/s)\n",
  702. (is_write)?"Wrote":"Read", file_size, buf_size, t_s * 1e3,
  703. file_size / (1024.0f * 1024.0f * t_s));
  704. }