added lua VM for installation, and changed the standard os library from lua
This commit is contained in:
@@ -531,21 +531,28 @@ func Install(packagepath string, serial uint) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bar.Finish()
|
|
||||||
|
|
||||||
// TODO LUA SCRIPT
|
// TODO LUA SCRIPT
|
||||||
|
|
||||||
L := lua.NewState()
|
L := lua.NewState()
|
||||||
defer L.Close()
|
defer L.Close()
|
||||||
|
|
||||||
|
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||||
|
|
||||||
L.SetGlobal("packets_package_dir", lua.LString(cfg.Config.DataDir))
|
L.SetGlobal("packets_package_dir", lua.LString(cfg.Config.DataDir))
|
||||||
L.SetGlobal("packets_bin_dir", lua.LString(cfg.Config.BinDir))
|
L.SetGlobal("packets_bin_dir", lua.LString(cfg.Config.BinDir))
|
||||||
L.SetField(L.GetGlobal("os"), "remove", L.NewFunction(internal.SafeRemove))
|
osObject.RawSetString("execute", lua.LNil)
|
||||||
|
osObject.RawSetString("exit", lua.LNil)
|
||||||
|
osObject.RawSetString("getenv", lua.LNil)
|
||||||
|
|
||||||
|
osObject.RawSetString("remove", L.NewFunction(internal.SafeRemove))
|
||||||
|
osObject.RawSetString("rename", L.NewFunction(internal.SafeRename))
|
||||||
|
osObject.RawSetString("copy", L.NewFunction(internal.SafeCopy))
|
||||||
|
|
||||||
if err := L.DoFile(manifest.Hooks.Install); err != nil {
|
if err := L.DoFile(manifest.Hooks.Install); err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bar.Finish()
|
||||||
fmt.Printf("Package %s fully installed\n", name)
|
fmt.Printf("Package %s fully installed\n", name)
|
||||||
|
|
||||||
var insert = Installed{
|
var insert = Installed{
|
||||||
@@ -1408,8 +1415,6 @@ func Upgrade(packagepath string, og_realname string, serial uint) error {
|
|||||||
|
|
||||||
os.Rename(destDir, filepath.Join(cfg.Config.DataDir, name))
|
os.Rename(destDir, filepath.Join(cfg.Config.DataDir, name))
|
||||||
|
|
||||||
destDir = filepath.Join(cfg.Config.DataDir, name)
|
|
||||||
|
|
||||||
//TODO manifest.toml things
|
//TODO manifest.toml things
|
||||||
|
|
||||||
fmt.Printf("Package %s fully installed", name)
|
fmt.Printf("Package %s fully installed", name)
|
||||||
|
|||||||
@@ -189,16 +189,83 @@ func IsSafe(str string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SafeRemove(L *lua.LState) int {
|
func SafeRemove(L *lua.LState) int {
|
||||||
path := L.CheckString(1)
|
filename := L.CheckString(1)
|
||||||
if !IsSafe(path) {
|
if !IsSafe(filename) {
|
||||||
L.Push(lua.LFalse)
|
L.Push(lua.LFalse)
|
||||||
return 1
|
L.Push(lua.LString("[packets] unsafe filepath"))
|
||||||
|
return 2
|
||||||
}
|
}
|
||||||
err := os.Remove(path)
|
err := os.Remove(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
L.Push(lua.LFalse)
|
L.Push(lua.LFalse)
|
||||||
|
L.Push(lua.LString("[packets] remove failed\n" + err.Error()))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
L.Push(lua.LTrue)
|
L.Push(lua.LTrue)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SafeRename(L *lua.LState) int {
|
||||||
|
oldname := L.CheckString(1)
|
||||||
|
newname := L.CheckString(2)
|
||||||
|
|
||||||
|
if !IsSafe(oldname) || !IsSafe(newname) {
|
||||||
|
L.Push(lua.LFalse)
|
||||||
|
L.Push(lua.LString("[packets] unsafe filepath"))
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Rename(oldname, newname); err != nil {
|
||||||
|
L.Push(lua.LFalse)
|
||||||
|
L.Push(lua.LString("[packets] rename failed\n" + err.Error()))
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
L.Push(lua.LTrue)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
func SafeCopy(L *lua.LState) int {
|
||||||
|
oldname := L.CheckString(1)
|
||||||
|
newname := L.CheckString(2)
|
||||||
|
|
||||||
|
if !IsSafe(oldname) || !IsSafe(newname) {
|
||||||
|
L.Push(lua.LFalse)
|
||||||
|
L.Push(lua.LString("[packets] unsafe filepath"))
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
src, err := os.Open(oldname)
|
||||||
|
if err != nil {
|
||||||
|
L.Push(lua.LFalse)
|
||||||
|
L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
|
||||||
|
return 2
|
||||||
|
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
|
||||||
|
err = os.MkdirAll(filepath.Dir(newname), 0755)
|
||||||
|
if err != nil {
|
||||||
|
L.Push(lua.LFalse)
|
||||||
|
L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
dst, err := os.Create(newname)
|
||||||
|
if err != nil {
|
||||||
|
L.Push(lua.LFalse)
|
||||||
|
L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
defer dst.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(dst, src)
|
||||||
|
if err != nil {
|
||||||
|
L.Push(lua.LFalse)
|
||||||
|
L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
L.Push(lua.LTrue)
|
||||||
|
L.Push(lua.LNil)
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user