-
Notifications
You must be signed in to change notification settings - Fork 13
/
file.lua
232 lines (184 loc) · 4.77 KB
/
file.lua
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <[email protected]>
local file = require 'eco.core.file'
local time = require 'eco.time'
local sys = require 'eco.sys'
local M = {}
function M.readfile(path, m)
local f, err = io.open(path, 'r')
if not f then
return nil, err
end
local data, err = f:read(m or '*a')
f:close()
if not data then
return nil, err
end
return data
end
function M.writefile(path, data, append)
local m = 'w'
if append then
m = 'a'
end
local f, err = io.open(path, m)
if not f then
return nil, err
end
local n, err = f:write(data)
f:close()
if not n then
return nil, err
end
return n
end
function M.flock(fd, operation, timeout)
local deadtime
if timeout then
deadtime = sys.uptime() + timeout
end
while true do
local ok, errno = file.flock(fd, operation)
if ok then
return true
end
if errno ~= sys.EAGAIN then
return false, sys.strerror(errno)
end
if deadtime and sys.uptime() > deadtime then
return false, 'timeout'
end
time.sleep(0.001)
end
end
function M.sync(timeout)
local p, err = sys.exec('sync')
if not p then
return nil, err
end
return p:wait(timeout)
end
local inotify_methods = {}
local function read_inotify_event(self, timeout)
local data = self.data
local wd, mask, _, len = string.unpack('I4I4I4I4', data)
local name
if len > 0 then
name = data:sub(16, len + 16):gsub('\0+', '')
end
data = data:sub(len + 17)
if #data > 0 then
self.data = data
else
self.data = nil
end
local path = self.watchs[wd]
if not path then
return self:wait(timeout)
end
if len > 0 then
if path:sub(#path) ~= '/' then
path = path .. '/'
end
name = path .. name
else
name = path
end
local event
if mask & file.IN_ACCESS > 0 then
event = 'ACCESS'
elseif mask & file.IN_MODIFY > 0 then
event = 'MODIFY'
elseif mask & file.IN_ATTRIB > 0 then
event = 'ATTRIB'
elseif mask & file.IN_CLOSE > 0 then
event = 'CLOSE'
elseif mask & file.IN_OPEN > 0 then
event = 'OPEN'
elseif mask & file.IN_MOVE > 0 then
event = 'MOVE'
elseif mask & file.IN_CREATE > 0 then
event = 'CREATE'
elseif mask & file.IN_DELETE > 0 then
event = 'DELETE'
elseif mask & file.IN_DELETE_SELF > 0 then
event = 'DELETE_SELF'
elseif mask & file.IN_MOVE_SELF > 0 then
event = 'MOVE_SELF'
end
if not event then
return self:wait(timeout)
end
return {
name = name,
event = event,
mask = mask
}
end
--[[
wait the next event and return a table represent an event containing the following fields.
name: filename associated with the event
event: event name, supports ACCESS, MODIFY, ATTRIB, CLOSE, OPEN, MOVE, CREATE, DELETE, DELETE_SELF, MOVE_SELF
mask: contains bits that describe the event that occurred
--]]
function inotify_methods:wait(timeout)
if self.data then
return read_inotify_event(self, timeout)
end
local ok, err = self.iow:wait(timeout)
if not ok then
return nil, err
end
local data, err = file.read(self.fd, 1024)
if not data then
return nil, err
end
self.data = data
return read_inotify_event(self, timeout)
end
-- add a watch to an initialized inotify instance
-- you can set events be of interest to you via the second argument, defaults to `file.IN_ALL_EVENTS`.
-- return the watch descriptor will be used in `del` method.
function inotify_methods:add(path, mask)
local wd, err = file.inotify_add_watch(self.fd, path, mask or file.IN_ALL_EVENTS)
if not wd then
return nil, err
end
self.watchs[wd] = path
return wd
end
-- remove an existing watch from an inotify instance
-- wd: the watch descriptor returned via `add`
function inotify_methods:del(wd)
local ok, err = file.inotify_rm_watch(self.fd, wd)
if not ok then
return nil, err
end
self.watchs[wd] = nil
return ok
end
function inotify_methods:close()
if self.fd < 0 then
return
end
self.iow:cancel()
file.close(self.fd)
self.fd = -1
end
local inotify_mt = {
__index = inotify_methods,
__gc = inotify_methods.close
}
-- create an inotify instance
function M.inotify()
local fd, err = file.inotify_init()
if not fd then
return nil, err
end
return setmetatable({
fd = fd,
watchs = {},
iow = eco.watcher(eco.IO, fd),
}, inotify_mt)
end
return setmetatable(M, { __index = file })