扩展阅读:
合宙Air103|SDIO (扩展) |LuatOS-SOC接口|官方demo|学习(15-3):SDIO - 文件读写操作_打酱油的工程师的博客-CSDN博客
合宙Air105|SFUD库|FS|SPI FLASH|W25QXX |LuatOS-SOC接口|官方demo|学习(15-4):SFUD库操作-外置flash_打酱油的工程师的博客-CSDN博客
-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "fsdemo"
VERSION = "1.0.0"
log.info("main", PROJECT, VERSION)
-- sys库是标配
_G.sys = require("sys")
--添加硬狗防止程序卡死
if wdt then
wdt.init(9000)--初始化watchdog设置为9s
sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
end
local function fs_test()
-- 根目录/是可写
local f = io.open("/boot_time", "rb")
local c = 0
if f then
local data = f:read("*a")
log.info("fs", "data", data, data:toHex())
c = tonumber(data)
f:close()
end
log.info("fs", "boot count", c)
c = c + 1
f = io.open("/boot_time", "wb")
--if f ~= nil then
log.info("fs", "write c to file", c, tostring(c))
f:write(tostring(c))
f:close()
--end
log.info("io.writeFile", io.writeFile("/abc.txt", "ABCDEFG"))
log.info("io.readFile", io.readFile("/abc.txt"))
local f = io.open("/abc.txt", "rb")
local c = 0
if f then
local data = f:read("*a")
log.info("fs", "data2", data, data:toHex())
f:close()
end
-- seek和tell测试
local f = io.open("/abc.txt", "rb")
local c = 0
if f then
--f:seek(io.SEEK_END,3) --从5号位搜索至文档末尾
--f:seek(io.SEEK_CUR,2) --从头开始,设置起始位置为2,
f:seek(io.SEEK_SET,1) --设置起始位置为1,
local data = f:read("*a")
log.info("fs", "data3", data, data:toHex())
f:close()
end
if fs then
-- 根目录是可读写的
log.info("fsstat", fs.fsstat("/"))
-- /luadb/ 是只读的
log.info("fsstat", fs.fsstat("/luadb/"))
end
local data = io.lsmount() --列出所有挂载点
--lsmount [{"fs":"lfs2","path":""},{"fs":"inline","path":"\/lua\/"},{"fs":"luadb","path":"\/luadb\/"}]
log.info("fs", "lsmount", json.encode(data))
--.fatfs lsdir [{"name":"abc.txt","type":0},{"name":"boot_time","type":0}]
local ret, files = io.lsdir("/") --列出根目录下的所有文件名
log.info("fatfs", "lsdir", json.encode(files))
-- 读取刷机时加入的文件, 并演示按行读取
-- 刷机时选取的非lua文件, 均存放在/luadb/目录下, 单层无子文件夹
f = io.open("/luadb/abc.txt", "rb")
if f then
while true do
local line = f:read("l")
if not line or #line == 0 then
break
end
log.info("fs", "read line", line)
end
f:close()
log.info("fs", "close f")
else
log.info("fs", "pls add abc.txt!!")
end
-- 文件夹操作
sys.wait(3000)
io.mkdir("/iot") --建立目录命令最后不带"/"
f = io.open("/iot/1.txt", "w+")
if f then
f:write("hi, LuatOS " .. os.date())
f:close()
else
log.info("fs", "open file for write failed")
end
f = io.open("/iot/1.txt", "r")
if f then
local data = f:read("*a")
f:close()
log.info("fs", "writed data", data)
else
log.info("fs", "open file for read failed")
end
end
sys.taskInit(function()
-- 为了显示日志,这里特意延迟一秒
-- 正常使用不需要delay
sys.wait(1000)
fs_test()
end)
-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
LOG:
[2023-02-09 21:48:14.537] main 188:APP Build release Jan 1 2023 16:20:44!
[2023-02-09 21:48:14.537] I/main LuatOS@AIR105 base 22.12 bsp V0013 32bit
[2023-02-09 21:48:14.537] I/main ROM Build: Jan 1 2023 16:20:52
[2023-02-09 21:48:14.537] D/main loadlibs luavm 204792 15376 15376
[2023-02-09 21:48:14.537] D/main loadlibs sys 397480 51072 51616
[2023-02-09 21:48:14.537] I/user.main fsdemo 1.0.0
[2023-02-09 21:48:15.562] I/user.fs data 22 3232 4
[2023-02-09 21:48:15.562] I/user.fs boot count 22
[2023-02-09 21:48:15.562] I/user.fs write c to file 23 23
[2023-02-09 21:48:15.593] I/user.io.writeFile true
[2023-02-09 21:48:15.593] I/user.io.readFile ABCDEFG
[2023-02-09 21:48:15.593] I/user.fs data2 ABCDEFG 41424344454647 14
[2023-02-09 21:48:15.593] I/user.fs data3 BCDEFG 424344454647 12
[2023-02-09 21:48:15.593] I/user.fsstat true 128 4 4096 lfs
[2023-02-09 21:48:15.593] I/user.fsstat true 0 0 512 luadb
[2023-02-09 21:48:15.593] I/user.fs lsmount [{"fs":"lfs2","path":""},{"fs":"inline","path":"\/lua\/"},{"fs":"luadb","path":"\/luadb\/"}]
[2023-02-09 21:48:15.593] I/user.fatfs lsdir [{"name":"abc.txt","type":0},{"name":"boot_time","type":0}]
[2023-02-09 21:48:15.593] I/user.fs read line ABC
[2023-02-09 21:48:15.593] I/user.fs read line DEF
[2023-02-09 21:48:15.602] I/user.fs read line XYZ
[2023-02-09 21:48:15.602] I/user.fs close f
[2023-02-09 21:48:18.622] I/user.fs writed data hi, LuatOS Sun Jan 1 16:20:50 2023
PS:fs - 文件系统额外操作
fs - 文件系统额外操作
已适配 Air101/Air103 Air105 ESP32C3 Air780
备注
本页文档由这个文件自动生成。如有错误,请提交issue或帮忙修改后pr,谢谢!
小技巧
本库有专属demo,点此链接查看fs的demo例子
fs.fsstat(path)
获取文件系统信息
参数
传入值类型 | 解释 |
string | 路径,默认”/”,可选 |
返回值
返回值类型 | 解释 |
boolean | 获取成功返回true,否则返回false |
int | 总的block数量 |
int | 已使用的block数量 |
int | block的大小,单位字节 |
string | 文件系统类型,例如lfs代表littlefs |
例子
-- 打印根分区的信息
log.info("fsstat", fs.fsstat("/"))
fs.fsize(path)
获取文件大小
参数
传入值类型 | 解释 |
string | 文件路径 |
返回值
返回值类型 | 解释 |
int | 文件大小,若获取失败会返回0 |
例子
-- 打印main.luac的大小
log.info("fsize", fs.fsize("/main.luac"))