LITTLEFS_test.ino 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include <Arduino.h>
  2. #include "FS.h"
  3. #include <LITTLEFS.h>
  4. /* You only need to format LITTLEFS the first time you run a
  5. test or else use the LITTLEFS plugin to create a partition
  6. https://github.com/lorol/arduino-esp32littlefs-plugin
  7. If you test two partitions, you need to use a custom
  8. partition.csv file, see in the sketch folder */
  9. //#define TWOPART
  10. #define FORMAT_LITTLEFS_IF_FAILED true
  11. void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
  12. Serial.printf("Listing directory: %s\r\n", dirname);
  13. File root = fs.open(dirname);
  14. if(!root){
  15. Serial.println("- failed to open directory");
  16. return;
  17. }
  18. if(!root.isDirectory()){
  19. Serial.println(" - not a directory");
  20. return;
  21. }
  22. File file = root.openNextFile();
  23. while(file){
  24. if(file.isDirectory()){
  25. Serial.print(" DIR : ");
  26. Serial.println(file.name());
  27. if(levels){
  28. listDir(fs, file.name(), levels -1);
  29. }
  30. } else {
  31. Serial.print(" FILE: ");
  32. Serial.print(file.name());
  33. Serial.print("\tSIZE: ");
  34. Serial.println(file.size());
  35. }
  36. file = root.openNextFile();
  37. }
  38. }
  39. void createDir(fs::FS &fs, const char * path){
  40. Serial.printf("Creating Dir: %s\n", path);
  41. if(fs.mkdir(path)){
  42. Serial.println("Dir created");
  43. } else {
  44. Serial.println("mkdir failed");
  45. }
  46. }
  47. void removeDir(fs::FS &fs, const char * path){
  48. Serial.printf("Removing Dir: %s\n", path);
  49. if(fs.rmdir(path)){
  50. Serial.println("Dir removed");
  51. } else {
  52. Serial.println("rmdir failed");
  53. }
  54. }
  55. void readFile(fs::FS &fs, const char * path){
  56. Serial.printf("Reading file: %s\r\n", path);
  57. File file = fs.open(path);
  58. if(!file || file.isDirectory()){
  59. Serial.println("- failed to open file for reading");
  60. return;
  61. }
  62. Serial.println("- read from file:");
  63. while(file.available()){
  64. Serial.write(file.read());
  65. }
  66. file.close();
  67. }
  68. void writeFile(fs::FS &fs, const char * path, const char * message){
  69. Serial.printf("Writing file: %s\r\n", path);
  70. File file = fs.open(path, FILE_WRITE);
  71. if(!file){
  72. Serial.println("- failed to open file for writing");
  73. return;
  74. }
  75. if(file.print(message)){
  76. Serial.println("- file written");
  77. } else {
  78. Serial.println("- write failed");
  79. }
  80. file.close();
  81. }
  82. void appendFile(fs::FS &fs, const char * path, const char * message){
  83. Serial.printf("Appending to file: %s\r\n", path);
  84. File file = fs.open(path, FILE_APPEND);
  85. if(!file){
  86. Serial.println("- failed to open file for appending");
  87. return;
  88. }
  89. if(file.print(message)){
  90. Serial.println("- message appended");
  91. } else {
  92. Serial.println("- append failed");
  93. }
  94. file.close();
  95. }
  96. void renameFile(fs::FS &fs, const char * path1, const char * path2){
  97. Serial.printf("Renaming file %s to %s\r\n", path1, path2);
  98. if (fs.rename(path1, path2)) {
  99. Serial.println("- file renamed");
  100. } else {
  101. Serial.println("- rename failed");
  102. }
  103. }
  104. void deleteFile(fs::FS &fs, const char * path){
  105. Serial.printf("Deleting file: %s\r\n", path);
  106. if(fs.remove(path)){
  107. Serial.println("- file deleted");
  108. } else {
  109. Serial.println("- delete failed");
  110. }
  111. }
  112. // SPIFFS-like write and delete file, better use #define CONFIG_LITTLEFS_SPIFFS_COMPAT 1
  113. void writeFile2(fs::FS &fs, const char * path, const char * message){
  114. if(!fs.exists(path)){
  115. if (strchr(path, '/')) {
  116. Serial.printf("Create missing folders of: %s\r\n", path);
  117. char *pathStr = strdup(path);
  118. if (pathStr) {
  119. char *ptr = strchr(pathStr, '/');
  120. while (ptr) {
  121. *ptr = 0;
  122. fs.mkdir(pathStr);
  123. *ptr = '/';
  124. ptr = strchr(ptr+1, '/');
  125. }
  126. }
  127. free(pathStr);
  128. }
  129. }
  130. Serial.printf("Writing file to: %s\r\n", path);
  131. File file = fs.open(path, FILE_WRITE);
  132. if(!file){
  133. Serial.println("- failed to open file for writing");
  134. return;
  135. }
  136. if(file.print(message)){
  137. Serial.println("- file written");
  138. } else {
  139. Serial.println("- write failed");
  140. }
  141. file.close();
  142. }
  143. void deleteFile2(fs::FS &fs, const char * path){
  144. Serial.printf("Deleting file and empty folders on path: %s\r\n", path);
  145. if(fs.remove(path)){
  146. Serial.println("- file deleted");
  147. } else {
  148. Serial.println("- delete failed");
  149. }
  150. char *pathStr = strdup(path);
  151. if (pathStr) {
  152. char *ptr = strrchr(pathStr, '/');
  153. if (ptr) {
  154. Serial.printf("Removing all empty folders on path: %s\r\n", path);
  155. }
  156. while (ptr) {
  157. *ptr = 0;
  158. fs.rmdir(pathStr);
  159. ptr = strrchr(pathStr, '/');
  160. }
  161. free(pathStr);
  162. }
  163. }
  164. void testFileIO(fs::FS &fs, const char * path){
  165. Serial.printf("Testing file I/O with %s\r\n", path);
  166. static uint8_t buf[512];
  167. size_t len = 0;
  168. File file = fs.open(path, FILE_WRITE);
  169. if(!file){
  170. Serial.println("- failed to open file for writing");
  171. return;
  172. }
  173. size_t i;
  174. Serial.print("- writing" );
  175. uint32_t start = millis();
  176. for(i=0; i<2048; i++){
  177. if ((i & 0x001F) == 0x001F){
  178. Serial.print(".");
  179. }
  180. file.write(buf, 512);
  181. }
  182. Serial.println("");
  183. uint32_t end = millis() - start;
  184. Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
  185. file.close();
  186. file = fs.open(path);
  187. start = millis();
  188. end = start;
  189. i = 0;
  190. if(file && !file.isDirectory()){
  191. len = file.size();
  192. size_t flen = len;
  193. start = millis();
  194. Serial.print("- reading" );
  195. while(len){
  196. size_t toRead = len;
  197. if(toRead > 512){
  198. toRead = 512;
  199. }
  200. file.read(buf, toRead);
  201. if ((i++ & 0x001F) == 0x001F){
  202. Serial.print(".");
  203. }
  204. len -= toRead;
  205. }
  206. Serial.println("");
  207. end = millis() - start;
  208. Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
  209. file.close();
  210. } else {
  211. Serial.println("- failed to open file for reading");
  212. }
  213. }
  214. void setup(){
  215. Serial.begin(115200);
  216. #ifdef TWOPART
  217. if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED, "/lfs2", 5, "part2")){
  218. Serial.println("part2 Mount Failed");
  219. return;
  220. }
  221. appendFile(LITTLEFS, "/hello0.txt", "World0!\r\n");
  222. readFile(LITTLEFS, "/hello0.txt");
  223. LITTLEFS.end();
  224. Serial.println( "Done with part2, work with the first lfs partition..." );
  225. #endif
  226. if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
  227. Serial.println("LITTLEFS Mount Failed");
  228. return;
  229. }
  230. Serial.println( "SPIFFS-like write file to new path and delete it w/folders" );
  231. writeFile2(LITTLEFS, "/new1/new2/new3/hello3.txt", "Hello3");
  232. listDir(LITTLEFS, "/", 3);
  233. deleteFile2(LITTLEFS, "/new1/new2/new3/hello3.txt");
  234. listDir(LITTLEFS, "/", 3);
  235. createDir(LITTLEFS, "/mydir");
  236. writeFile(LITTLEFS, "/mydir/hello2.txt", "Hello2");
  237. listDir(LITTLEFS, "/", 1);
  238. deleteFile(LITTLEFS, "/mydir/hello2.txt");
  239. removeDir(LITTLEFS, "/mydir");
  240. listDir(LITTLEFS, "/", 1);
  241. writeFile(LITTLEFS, "/hello.txt", "Hello ");
  242. appendFile(LITTLEFS, "/hello.txt", "World!\r\n");
  243. readFile(LITTLEFS, "/hello.txt");
  244. renameFile(LITTLEFS, "/hello.txt", "/foo.txt");
  245. readFile(LITTLEFS, "/foo.txt");
  246. deleteFile(LITTLEFS, "/foo.txt");
  247. testFileIO(LITTLEFS, "/test.txt");
  248. deleteFile(LITTLEFS, "/test.txt");
  249. Serial.println( "Test complete" );
  250. }
  251. void loop(){
  252. }