Skip to content

Commit

Permalink
Fine-tuning file_lfs module
Browse files Browse the repository at this point in the history
  • Loading branch information
vsky279 committed Feb 12, 2021
1 parent dc9a114 commit 80482e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
62 changes: 35 additions & 27 deletions docs/lua-modules/file_lfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,13 @@ Both basic and object model can be used to access LFS file (see [file](../../doc

Files to be stored in LFS needs to be preprocessed, i.e. a Lua file with its contents needs to be generated. This file called `resource.lua` is then included in the LFS image. A Lua script [`make_resource.lua`](../../lua_modules/file_lfs/make_resource.lua) can be used to generate `resource.lua` script.

A structure of the `resource.lua` file is simple. It returns a string, i.e. file content, depending on filename parameter passed to it. It returns table with list of files stored when called without any parameter. Simple example:
A structure of the `resource.lua` file is simple. It returns a string, i.e. file content, depending on filename parameter passed to it. It returns table with list of files stored when called without any parameter.
```Lua
local arg = ...
if arg == "index.html" then return "<!DOCTYPE html><html><body>Hi, there!</body></html>" end
if arg == "favicon.ico" then return ""\000\000\000\000\000\000..." end
if arg == nil then return {"index.html", "favicon.ico"} end
```
or as generated by `make_resource.lua` script:
```Lua
local arg = ...
local table_insert=table.insert
local filelist={}
table_insert(filelist, "index.html")
if arg == "index.html" then return "<!DOCTYPE html><html><body>Hi, there!</body></html>" end
table_insert(filelist, "favicon.ico")
if arg == "favicon.ico" then return ""\000\000\000\000\000\000..." end
if arg == nil then return filelist end
```

## `make_resource.lua` script

Expand Down Expand Up @@ -65,6 +54,39 @@ f:close()
Methods implemented - basically all `file` module functions are available though only some of them work with LFS files. The other functions are just passed through to the base `file` functions.
## file_lfs.list()
Lists all files in the file system. It works almost in the same way as [`file.list()`](../../docs/modules/file.md#filelist)
#### Syntax
`file.list([pattern], [SPIFFs_only])`
#### Parameters
- `pattern` only files matching the Lua pattern will be returned
- `SPIFFs_only` if not `nil` LFS files won't be included in the result (LFS files are returned only if the parameter is `nil`)
#### Returns
a Lua table which contains all {file name: file size} pairs, if no pattern
given. If a pattern is given, only those file names matching the pattern
(interpreted as a traditional [Lua pattern](https://www.lua.org/pil/20.2.html),
not, say, a UNIX shell glob) will be included in the resulting table.
`file.list` will throw any errors encountered during pattern matching.
## file.rename()
Renames a file. If a file is currently open, it will be closed first. It works almost in the same way as [`file.rename()`](../../docs/modules/file.md#filerename)
#### Syntax
`file.rename(oldname, newname)`
#### Parameters
- `oldname` old file name
- `newname` new file name
#### Returns
`true` on success, `false` when the file is stored in LFS (so read-only) or on error
## file_lfs.open()
Opens a LFS file if it is included in LFS in the `resource.lua` file. If not standard [`file.open()`](../../docs/modules/file.md#fileopen-fileobjopen) function is called.
Expand All @@ -83,23 +105,9 @@ LFS file is opened only when "r" access is requested.
#### Returns
LFS file object (Lua table) or SPIFFS file object if file opened ok. `nil` if file not opened, or not exists (read modes).
## file_lfs.exists()
Checks whether the LFS or SPIFFS file exists.
#### Syntax
`file.exists(filename)`
#### Parameters
- `filename` file to check
#### Returns
`true` if the file exists (even if 0 bytes in size), and `false` if it does not exist
## file.read(), file.obj:read()
Read content from the open file. It has the same parameters and return values as [`file.read()` / `file.obj:read()`](../../docs/modules/file.md#fileopen-fileobjopen#fileread-fileobjread)
Read content from the open file. It has the same parameters and returns values as [`file.read()` / `file.obj:read()`](../../docs/modules/file.md#fileread-fileobjread)
#### Syntax
`file.read([n_or_char])`
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Lists all files in the file system.
`file.list([pattern])`

#### Parameters
none
- `pattern` only files matching the Lua pattern will be returned

#### Returns
a Lua table which contains all {file name: file size} pairs, if no pattern
Expand Down
12 changes: 7 additions & 5 deletions lua_modules/file_lfs/file_lfs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ file_lfs.stat = function(filename)
end
end

file_lfs.list = function (pattern)
file_lfs.list = function (pattern, SPIFFs_only)
local filelist = file_list(pattern)
local fl = node_LFS_resource()
for _, f in ipairs(fl) do
if f:match(pattern or ".*") and not(filelist[f]) then
filelist[f] = #node_LFS_resource(f)
if not SPIFFs_only then
local fl = node_LFS_resource()
for _, f in ipairs(fl) do
if f:match(pattern or ".*") and not(filelist[f]) then
filelist[f] = #node_LFS_resource(f)
end
end
end
return filelist
Expand Down
12 changes: 7 additions & 5 deletions lua_modules/file_lfs/make_resource.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ end

-- tests the functions above
print(string.format("make_resource script - %d parameter(s)", #arg))
print("parameters: [-o outputfile] file1 [file2] ...")
if #arg==0 or arg[1]=="--help" then
print("parameters: [-o outputfile] file1 [file2] ...")
return
end

local larg = {}
local outpar = false
Expand All @@ -42,10 +45,9 @@ print(string.format("output set to: %s", OUT))

local res = io.open(OUT, "w")
res:write("-- luacheck: max line length no\nlocal arg = ...\n")
res:write(('local table_insert=table.insert\n'):format(inp, content))
res:write(('local filelist={}\n\n'):format(inp, content))
res:close(file)

local filelist = ""
for _, a in pairs(larg) do
local inp = string.match(a, ".*[/](.*)")
if not inp then inp = a end
Expand All @@ -54,12 +56,12 @@ for _, a in pairs(larg) do

if content then
res = io.open(OUT, "a")
res:write(('table_insert(filelist, "%s")\n'):format(inp, content))
filelist = filelist .. ('"%s",'):format(inp)
res:write(('if arg == "%s" then return %q end\n\n'):format(inp, content))
res:close(file)
end
end

res = io.open(OUT, "a")
res:write('if arg == nil then return filelist end\n')
res:write(('if arg == nil then return {%s} end\n'):format(filelist))
res:close(file)

0 comments on commit 80482e8

Please sign in to comment.