-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapi.go
172 lines (150 loc) · 3.03 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package lfs
import (
"os"
"time"
"github.com/yuin/gopher-lua"
)
var api = map[string]lua.LGFunction{
"attributes": apiAttributes,
"chdir": apiChdir,
"lock_dir": apiLockdir,
"currentdir": apiCurrentdir,
"dir": apiDir,
"lock": apiLock,
"link": apiLink,
"mkdir": apiMkdir,
"rmdir": apiRmdir,
"setmode": apiSetmode,
"symlinkattributes": apiSymlinkattributes,
"touch": apiTouch,
"unlock": apiUnlock,
}
func apiAttributes(L *lua.LState) int {
return attributes(L, os.Stat)
}
func apiChdir(L *lua.LState) int {
dir := L.CheckString(1)
err := os.Chdir(dir)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
return 1
}
func apiLockdir(L *lua.LState) int {
L.RaiseError("unimplemented function")
return 0
}
func apiCurrentdir(L *lua.LState) int {
dir, err := os.Getwd()
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LString(dir))
return 1
}
func apiDir(L *lua.LState) int {
path := L.CheckString(1)
f, err := os.Open(path)
if err != nil {
L.RaiseError("%s", err)
return 0
}
stat, err := f.Stat()
if err != nil {
L.RaiseError("%s", err)
return 0
}
if !stat.IsDir() {
L.RaiseError("not a directory")
return 0
}
L.Push(L.NewFunction(dirItr))
ud := L.NewUserData()
ud.Value = f
L.Push(ud)
return 2
}
func apiLock(L *lua.LState) int {
L.RaiseError("unimplemented function")
return 0
}
func apiLink(L *lua.LState) int {
old := L.CheckString(1)
new := L.CheckString(2)
symlink := L.OptBool(3, false)
var err error
if symlink {
err = os.Symlink(old, new)
} else {
err = os.Link(old, new)
}
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
return 1
}
func apiMkdir(L *lua.LState) int {
dir := L.CheckString(1)
err := os.Mkdir(dir, 0755)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
return 1
}
func apiRmdir(L *lua.LState) int {
dir := L.CheckString(1)
stat, err := os.Stat(dir)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
if !stat.IsDir() {
L.Push(lua.LNil)
L.Push(lua.LString("not a directory"))
return 2
}
err = os.Remove(dir)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
return 1
}
func apiSetmode(L *lua.LState) int {
L.RaiseError("unimplemented function")
return 0
}
func apiSymlinkattributes(L *lua.LState) int {
return attributes(L, os.Lstat)
}
func apiTouch(L *lua.LState) int {
filepath := L.CheckString(1)
atime := L.OptInt64(2, time.Now().Unix())
mtime := L.OptInt64(3, atime)
err := os.Chtimes(filepath, time.Unix(atime, 0), time.Unix(mtime, 0))
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LTrue)
return 1
}
func apiUnlock(L *lua.LState) int {
L.RaiseError("unimplemented function")
return 0
}