2021-03-20 22:49:15 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2022-07-13 19:38:07 +00:00
|
|
|
"fmt"
|
2022-04-23 03:35:26 +00:00
|
|
|
"path/filepath"
|
2021-10-17 16:56:45 +00:00
|
|
|
"strconv"
|
2021-03-20 22:49:15 +00:00
|
|
|
"os"
|
2021-03-31 17:46:49 +00:00
|
|
|
"strings"
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2021-10-16 16:40:53 +00:00
|
|
|
"hilbish/util"
|
2022-04-04 10:40:02 +00:00
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
|
|
|
"github.com/arnodel/golua/lib/packagelib"
|
2021-03-20 22:49:15 +00:00
|
|
|
)
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
var Loader = packagelib.Loader{
|
|
|
|
Load: loaderFunc,
|
|
|
|
Name: "fs",
|
|
|
|
}
|
|
|
|
|
|
|
|
func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
|
|
|
exports := map[string]util.LuaExport{
|
|
|
|
"cd": util.LuaExport{fcd, 1, false},
|
|
|
|
"mkdir": util.LuaExport{fmkdir, 2, false},
|
|
|
|
"stat": util.LuaExport{fstat, 1, false},
|
|
|
|
"readdir": util.LuaExport{freaddir, 1, false},
|
2022-04-23 03:35:26 +00:00
|
|
|
"abs": util.LuaExport{fabs, 1, false},
|
2022-06-20 20:47:16 +00:00
|
|
|
"basename": util.LuaExport{fbasename, 1, false},
|
|
|
|
"dir": util.LuaExport{fdir, 1, false},
|
|
|
|
"glob": util.LuaExport{fglob, 1, false},
|
2022-07-13 19:38:07 +00:00
|
|
|
"join": util.LuaExport{fjoin, 0, true},
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
|
|
|
mod := rt.NewTable()
|
|
|
|
util.SetExports(rtm, mod, exports)
|
2022-06-20 20:47:16 +00:00
|
|
|
mod.Set(rt.StringValue("pathSep"), rt.StringValue(string(os.PathSeparator)))
|
|
|
|
mod.Set(rt.StringValue("pathListSep"), rt.StringValue(string(os.PathListSeparator)))
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
util.Document(mod, `The fs module provides easy and simple access to
|
2021-10-16 19:36:30 +00:00
|
|
|
filesystem functions and other things, and acts an
|
2022-05-01 04:49:59 +00:00
|
|
|
addition to the Lua standard library's I/O and filesystem functions.`)
|
2021-10-16 16:40:53 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return rt.TableValue(mod), nil
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// cd(dir)
|
|
|
|
// Changes directory to `dir`
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param dir string
|
2022-04-04 10:40:02 +00:00
|
|
|
func fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-01 04:49:59 +00:00
|
|
|
path = util.ExpandHome(strings.TrimSpace(path))
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2022-05-01 04:49:59 +00:00
|
|
|
err = os.Chdir(path)
|
2021-03-31 03:56:37 +00:00
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
2021-03-31 03:56:37 +00:00
|
|
|
}
|
2021-03-20 22:49:15 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), err
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// mkdir(name, recursive)
|
|
|
|
// Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param name string
|
2022-02-25 22:17:04 +00:00
|
|
|
// --- @param recursive boolean
|
2022-04-04 10:40:02 +00:00
|
|
|
func fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.CheckNArgs(2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-01 04:49:59 +00:00
|
|
|
path, err := c.StringArg(0)
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
recursive, err := c.BoolArg(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-01 04:49:59 +00:00
|
|
|
path = util.ExpandHome(strings.TrimSpace(path))
|
2021-04-09 22:10:18 +00:00
|
|
|
|
2021-06-12 13:31:42 +00:00
|
|
|
if recursive {
|
2021-10-16 17:49:01 +00:00
|
|
|
err = os.MkdirAll(path, 0744)
|
2021-06-12 13:31:42 +00:00
|
|
|
} else {
|
2021-10-16 17:49:01 +00:00
|
|
|
err = os.Mkdir(path, 0744)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
2021-06-12 13:31:42 +00:00
|
|
|
}
|
2021-04-09 22:10:18 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), err
|
2021-04-09 22:10:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// stat(path)
|
|
|
|
// Returns info about `path`
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param path string
|
2022-04-04 10:40:02 +00:00
|
|
|
func fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-01 04:49:59 +00:00
|
|
|
path = util.ExpandHome(path)
|
2021-04-09 22:10:18 +00:00
|
|
|
|
2021-10-16 17:47:39 +00:00
|
|
|
pathinfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
2021-10-16 17:47:39 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
statTbl := rt.NewTable()
|
|
|
|
statTbl.Set(rt.StringValue("name"), rt.StringValue(pathinfo.Name()))
|
|
|
|
statTbl.Set(rt.StringValue("size"), rt.IntValue(pathinfo.Size()))
|
|
|
|
statTbl.Set(rt.StringValue("mode"), rt.StringValue("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8)))
|
|
|
|
statTbl.Set(rt.StringValue("isDir"), rt.BoolValue(pathinfo.IsDir()))
|
|
|
|
|
|
|
|
return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil
|
2021-04-09 22:10:18 +00:00
|
|
|
}
|
2021-10-16 17:47:39 +00:00
|
|
|
|
|
|
|
// readdir(dir)
|
|
|
|
// Returns a table of files in `dir`
|
2022-02-25 22:15:25 +00:00
|
|
|
// --- @param dir string
|
|
|
|
// --- @return table
|
2022-04-04 10:40:02 +00:00
|
|
|
func freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dir, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-01 04:49:59 +00:00
|
|
|
dir = util.ExpandHome(dir)
|
2022-04-04 10:40:02 +00:00
|
|
|
names := rt.NewTable()
|
2021-10-16 17:47:39 +00:00
|
|
|
|
|
|
|
dirEntries, err := os.ReadDir(dir)
|
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
2021-10-16 17:47:39 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
for i, entry := range dirEntries {
|
|
|
|
names.Set(rt.IntValue(int64(i + 1)), rt.StringValue(entry.Name()))
|
2021-10-16 17:47:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.TableValue(names)), nil
|
2021-10-16 17:47:39 +00:00
|
|
|
}
|
2022-04-23 03:35:26 +00:00
|
|
|
|
|
|
|
// abs(path)
|
|
|
|
// Gives an absolute version of `path`.
|
|
|
|
// --- @param path string
|
|
|
|
func fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
path, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-01 04:49:59 +00:00
|
|
|
path = util.ExpandHome(path)
|
2022-04-23 03:35:26 +00:00
|
|
|
|
|
|
|
abspath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(abspath)), nil
|
|
|
|
}
|
2022-06-20 20:47:16 +00:00
|
|
|
|
|
|
|
// basename(path)
|
|
|
|
// Gives the basename of `path`. For the rules,
|
|
|
|
// see Go's filepath.Base
|
|
|
|
func fbasename(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.PushingNext(t.Runtime, rt.StringValue(filepath.Base(path))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dir(path)
|
|
|
|
// Returns the directory part of `path`. For the rules, see Go's
|
|
|
|
// filepath.Dir
|
|
|
|
func fdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.PushingNext(t.Runtime, rt.StringValue(filepath.Dir(path))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// glob(pattern)
|
|
|
|
// Glob all files and directories that match the pattern.
|
|
|
|
// For the rules, see Go's filepath.Glob
|
|
|
|
func fglob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pattern, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
luaMatches := rt.NewTable()
|
|
|
|
|
|
|
|
for i, match := range matches {
|
|
|
|
luaMatches.Set(rt.IntValue(int64(i + 1)), rt.StringValue(match))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.PushingNext(t.Runtime, rt.TableValue(luaMatches)), nil
|
|
|
|
}
|
2022-07-13 19:38:07 +00:00
|
|
|
|
|
|
|
// join(paths...)
|
|
|
|
// Takes paths and joins them together with the OS's
|
|
|
|
// directory separator (forward or backward slash).
|
|
|
|
func fjoin(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
strs := make([]string, len(c.Etc()))
|
|
|
|
for i, v := range c.Etc() {
|
|
|
|
if v.Type() != rt.StringType {
|
|
|
|
// +2; go indexes of 0 and first arg from above
|
|
|
|
return nil, fmt.Errorf("bad argument #%d to run (expected string, got %s)", i + 1, v.TypeName())
|
|
|
|
}
|
|
|
|
strs[i] = v.AsString()
|
|
|
|
}
|
|
|
|
|
|
|
|
res := filepath.Join(strs...)
|
|
|
|
|
|
|
|
return c.PushingNext(t.Runtime, rt.StringValue(res)), nil
|
|
|
|
}
|