fix(fs): make stat better

return a table with 4 values: name, size, mode and isDir
name -> name of path
size -> size (obviously)
mode -> permissions mode in a string as octal format
isDir -> whether path is a directory
pull/78/head
sammyette 2021-10-17 12:56:45 -04:00
parent df70082a81
commit 6b065dc035
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
1 changed files with 7 additions and 2 deletions

View File

@ -3,6 +3,7 @@
package fs package fs
import ( import (
"strconv"
"os" "os"
"strings" "strings"
@ -72,13 +73,17 @@ func fmkdir(L *lua.LState) int {
func fstat(L *lua.LState) int { func fstat(L *lua.LState) int {
path := L.CheckString(1) path := L.CheckString(1)
// TODO: handle error here
pathinfo, err := os.Stat(path) pathinfo, err := os.Stat(path)
if err != nil { if err != nil {
luaErr(L, err.Error()) luaErr(L, err.Error())
return 0 return 0
} }
L.Push(luar.New(L, pathinfo)) statTbl := L.NewTable()
L.SetField(statTbl, "name", lua.LString(pathinfo.Name()))
L.SetField(statTbl, "size", lua.LNumber(pathinfo.Size()))
L.SetField(statTbl, "mode", lua.LString("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8)))
L.SetField(statTbl, "isDir", lua.LBool(pathinfo.IsDir()))
L.Push(statTbl)
return 1 return 1
} }