flash_fs_io.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. --[[
  2. @module flash_fs_io
  3. @summary 内置Flash文件系统操作测试模块
  4. @version 1.0.0
  5. @date 2025.09.23
  6. @author 王棚嶙
  7. @usage
  8. 本文件为内置Flash文件系统的操作测试流程:
  9. 1. 获取文件系统信息( io.fsstat)
  10. 2. 创建目录
  11. 3. 创建并写入文件
  12. 4. 检查文件是否存在
  13. 5. 获取文件大小(io.fileSize)
  14. 6. 读取文件内容
  15. 7. 启动计数文件操作
  16. 8. 文件追加测试
  17. 9. 按行读取测试
  18. 10. 文件重命名
  19. 11. 列举目录内容
  20. 12. 删除文件
  21. 13. 删除目录
  22. 本文件没有对外接口,直接在main.lua中require "flash_fs_io"就可以加载运行
  23. ]]
  24. function flash_fs_io_task()
  25. -- 使用内置Flash文件系统,根目录为"/",
  26. local base_path = "/"
  27. -- 创建一个目录
  28. local demo_dir = "flash_demo"
  29. -- 文件名
  30. local dir_path = base_path .. demo_dir
  31. -- ########## 开始进行内置Flash文件系统操作 ##########
  32. log.info("文件系统操作", "===== 开始文件系统操作 =====")
  33. -- 1. 获取文件系统信息 (使用 io.fsstat接口)
  34. local success, total_blocks, used_blocks, block_size, fs_type = io.fsstat(base_path)
  35. if success then
  36. log.info(" io.fsstat成功:",
  37. "总空间=" .. total_blocks .. "块",
  38. "已用=" .. used_blocks .. "块",
  39. "块大小=" .. block_size.."字节",
  40. "类型=" .. fs_type)
  41. else
  42. log.error(" io.fsstat", "获取文件系统信息失败")
  43. return
  44. end
  45. -- 2. 创建目录
  46. -- 如果目录不存在,则创建目录
  47. if not io.dexist(dir_path) then
  48. -- 创建目录
  49. if io.mkdir(dir_path) then
  50. log.info("io.mkdir", "目录创建成功", "路径:" .. dir_path)
  51. else
  52. log.error("io.mkdir", "目录创建失败", "路径:" .. dir_path)
  53. return
  54. end
  55. else
  56. log.warn("io.mkdir", "目录已存在,跳过创建", "路径:" .. dir_path)
  57. end
  58. -- 3. 创建并写入文件
  59. local file_path = dir_path .. "/boottime"
  60. local file = io.open(file_path, "wb")
  61. if file then
  62. file:write("这是内置Flash文件系统API文档示例的测试内容")
  63. file:close()
  64. log.info("文件创建", "文件写入成功", "路径:" .. file_path)
  65. else
  66. log.error("文件创建", "文件创建失败", "路径:" .. file_path)
  67. return
  68. end
  69. -- 4. 检查文件是否存在
  70. if io.exists(file_path) then
  71. log.info("io.exists", "文件存在", "路径:" .. file_path)
  72. else
  73. log.error("io.exists", "文件不存在", "路径:" .. file_path)
  74. return
  75. end
  76. -- 5. 获取文件大小 (使用io.fileSize接口)
  77. local file_size = io.fileSize(file_path)
  78. if file_size then
  79. log.info("io.fileSize", "文件大小:" .. file_size .. "字节", "路径:" .. file_path)
  80. else
  81. log.error("io.fileSize", "获取文件大小失败", "路径:" .. file_path)
  82. return
  83. end
  84. -- 6. 读取文件内容
  85. file = io.open(file_path, "rb")
  86. if file then
  87. local content = file:read("*a")
  88. log.info("文件读取", "路径:" .. file_path, "内容:" .. content)
  89. file:close()
  90. else
  91. log.error("文件操作", "无法打开文件读取内容", "路径:" .. file_path)
  92. return
  93. end
  94. -- 7. 启动计数文件操作
  95. local count = 0
  96. file = io.open(file_path, "rb")
  97. if file then
  98. local data = file:read("*a")
  99. log.info("启动计数", "文件内容:", data, "十六进制:", data:toHex())
  100. count = tonumber(data) or 0
  101. file:close()
  102. else
  103. log.warn("启动计数", "文件不存在或无法打开")
  104. end
  105. log.info("启动计数", "当前值:", count)
  106. count = count + 1
  107. log.info("启动计数", "更新值:", count)
  108. file = io.open(file_path, "wb")
  109. if file then
  110. file:write(tostring(count))
  111. file:close()
  112. log.info("文件写入", "路径:" .. file_path, "内容:", count)
  113. else
  114. log.error("文件写入", "无法打开文件", "路径:" .. file_path)
  115. return
  116. end
  117. -- 8. 文件追加测试
  118. local append_file = dir_path .. "/test_a"
  119. os.remove(append_file) -- 清理旧文件
  120. file = io.open(append_file, "wb")
  121. if file then
  122. file:write("ABC")
  123. file:close()
  124. log.info("文件创建", "路径:" .. append_file, "初始内容:ABC")
  125. else
  126. log.error("文件创建", "无法创建文件", "路径:" .. append_file)
  127. return
  128. end
  129. file = io.open(append_file, "a+")
  130. if file then
  131. file:write("def")
  132. file:close()
  133. log.info("文件追加", "路径:" .. append_file, "追加内容:def")
  134. else
  135. log.error("文件追加", "无法打开文件进行追加", "路径:" .. append_file)
  136. return
  137. end
  138. -- 验证追加结果
  139. file = io.open(append_file, "r")
  140. if file then
  141. local data = file:read("*a")
  142. log.info("文件验证", "路径:" .. append_file, "内容:" .. data, "结果:",
  143. data == "ABCdef" and "成功" or "失败")
  144. file:close()
  145. else
  146. log.error("文件验证", "无法打开文件进行验证", "路径:" .. append_file)
  147. return
  148. end
  149. -- 9. 按行读取测试
  150. local line_file = dir_path .. "/testline"
  151. file = io.open(line_file, "w")
  152. if file then
  153. file:write("abc\n")
  154. file:write("123\n")
  155. file:write("wendal\n")
  156. file:close()
  157. log.info("文件创建", "路径:" .. line_file, "写入3行文本")
  158. else
  159. log.error("文件创建", "无法创建文件", "路径:" .. line_file)
  160. return
  161. end
  162. file = io.open(line_file, "r")
  163. if file then
  164. log.info("按行读取", "路径:" .. line_file, "第1行:", file:read("*l"))
  165. log.info("按行读取", "路径:" .. line_file, "第2行:", file:read("*l"))
  166. log.info("按行读取", "路径:" .. line_file, "第3行:", file:read("*l"))
  167. file:close()
  168. else
  169. log.error("按行读取", "无法打开文件", "路径:" .. line_file)
  170. return
  171. end
  172. -- 10. 文件重命名
  173. local old_path = append_file
  174. local new_path = dir_path .. "/renamed_file.txt"
  175. local success, err = os.rename(old_path, new_path)
  176. if success then
  177. log.info("os.rename", "文件重命名成功", "原路径:" .. old_path, "新路径:" .. new_path)
  178. -- 验证重命名结果
  179. if io.exists(new_path) and not io.exists(old_path) then
  180. log.info("验证结果", "重命名验证成功", "新文件存在", "原文件不存在")
  181. else
  182. log.error("验证结果", "重命名验证失败")
  183. end
  184. else
  185. log.error("os.rename", "重命名失败", "错误:" .. tostring(err), "原路径:" .. old_path)
  186. return
  187. end
  188. -- 11. 列举目录内容
  189. log.info("目录操作", "===== 开始目录列举 =====")
  190. local ret, data = io.lsdir(dir_path, 50, 0)
  191. if ret then
  192. log.info("fs", "lsdir", json.encode(data))
  193. else
  194. log.info("fs", "lsdir", "fail", ret, data)
  195. return
  196. end
  197. -- 12. 删除文件测试
  198. if os.remove(new_path) then
  199. log.info("os.remove", "文件删除成功", "路径:" .. new_path)
  200. if not io.exists(new_path) then
  201. log.info("验证结果", "renamed_file.txt文件删除验证成功")
  202. else
  203. log.error("验证结果", "renamed_file.txt文件删除验证失败")
  204. end
  205. else
  206. log.error("os.remove", "renamed_file.txt文件删除失败", "路径:" .. new_path)
  207. return
  208. end
  209. if os.remove(line_file) then
  210. log.info("os.remove", "testline文件删除成功", "路径:" .. line_file)
  211. if not io.exists(line_file) then
  212. log.info("验证结果", "testline文件删除验证成功")
  213. else
  214. log.error("验证结果", "testline文件删除验证失败")
  215. end
  216. else
  217. log.error("os.remove", "testline文件删除失败", "路径:" .. line_file)
  218. return
  219. end
  220. if os.remove(file_path) then
  221. log.info("os.remove", "文件删除成功", "路径:" .. file_path)
  222. if not io.exists(file_path) then
  223. log.info("验证结果", "boottime文件删除验证成功")
  224. else
  225. log.error("验证结果", "boottime文件删除验证失败")
  226. end
  227. else
  228. log.error("os.remove", "boottime文件删除失败", "路径:" .. file_path)
  229. return
  230. end
  231. -- 13. 删除目录
  232. if io.rmdir(dir_path) then
  233. log.info("io.rmdir", "目录删除成功", "路径:" .. dir_path)
  234. if not io.dexist(dir_path) then
  235. log.info("验证结果", "目录删除验证成功")
  236. else
  237. log.error("验证结果", "目录删除验证失败")
  238. end
  239. else
  240. log.error("io.rmdir", "目录删除失败", "路径:" .. dir_path)
  241. return
  242. end
  243. -- 再次获取文件系统信息,查看空间变化
  244. local final_success, final_total_blocks, final_used_blocks, final_block_size, final_fs_type = io.fsstat(base_path)
  245. if final_success then
  246. log.info(" io.fsstat", "操作后文件系统信息:",
  247. "总空间=" .. final_total_blocks .. "块",
  248. "已用=" .. final_used_blocks .. "块",
  249. "块大小=" .. final_block_size.."字节",
  250. "类型=" .. final_fs_type)
  251. end
  252. log.info("文件系统操作", "===== 文件系统操作完成 =====")
  253. end
  254. sys.taskInit(flash_fs_io_task)