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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
-- EFL LuaJIT bindings: Eina (file module)
-- For use with Elua
local ffi = require("ffi")
local C = ffi.C
local iterator = require("eina.iterator")
require("eina.xattr")
ffi.cdef [[
typedef unsigned char Eina_Bool;
typedef struct _Eina_File_Direct_Info Eina_File_Direct_Info;
typedef struct _Eina_Stat Eina_Stat;
typedef struct _Eina_File_Line Eina_File_Line;
typedef void (*Eina_File_Dir_List_Cb)(const char *name, const char *path,
void *data);
typedef Eina_Bool (*Eina_File_Copy_Progress)(void *data,
unsigned long long done, unsigned long long total);
typedef enum {
EINA_FILE_UNKNOWN,
EINA_FILE_FIFO,
EINA_FILE_CHR,
EINA_FILE_DIR,
EINA_FILE_BLK,
EINA_FILE_REG,
EINA_FILE_LNK,
EINA_FILE_SOCK,
EINA_FILE_WHT
} Eina_File_Type;
typedef struct _Eina_File Eina_File;
typedef enum {
EINA_FILE_RANDOM,
EINA_FILE_SEQUENTIAL,
EINA_FILE_WILLNEED,
EINA_FILE_POPULATE,
EINA_FILE_DONTNEED,
EINA_FILE_REMOVE
} Eina_File_Populate;
struct _Eina_File_Direct_Info {
size_t path_length;
size_t name_length;
size_t name_start;
Eina_File_Type type;
char path[8192];
};
struct _Eina_Stat {
unsigned long int dev;
unsigned long int ino;
unsigned int mode;
unsigned int nlink;
unsigned int uid;
unsigned int gid;
unsigned long int rdev;
unsigned long int size;
unsigned long int blksize;
unsigned long int blocks;
unsigned long int atime;
unsigned long int atimensec;
unsigned long int mtime;
unsigned long int mtimensec;
unsigned long int ctime;
unsigned long int ctimensec;
};
struct _Eina_File_Line {
const char *start;
const char *end;
unsigned int index;
unsigned long long length;
};
EAPI Eina_Bool eina_file_dir_list(const char *dir, Eina_Bool recursive,
Eina_File_Dir_List_Cb cb, void *data);
EAPI Eina_Iterator *eina_file_ls(const char *dir);
EAPI Eina_Iterator *eina_file_stat_ls(const char *dir);
EAPI int eina_file_statat(void *container, Eina_File_Direct_Info *info,
Eina_Stat *buf);
EAPI Eina_Iterator *eina_file_direct_ls(const char *dir);
EAPI char *eina_file_path_sanitize(const char *path);
typedef enum {
EINA_FILE_COPY_DATA = 0,
EINA_FILE_COPY_PERMISSION = (1 << 0),
EINA_FILE_COPY_XATTR = (1 << 1)
} Eina_File_Copy_Flags;
EAPI Eina_Bool eina_file_copy(const char *src, const char *dst,
Eina_File_Copy_Flags flags, Eina_File_Copy_Progress cb,
const void *cb_data);
EAPI Eina_File *eina_file_open(const char *name, Eina_Bool shared);
EAPI Eina_File *eina_file_virtualize(const char *virtual_name,
const void *data, unsigned long long length, Eina_Bool copy);
EAPI Eina_Bool eina_file_virtual(Eina_File *file);
EAPI Eina_Bool eina_file_refresh(Eina_File *file);
EAPI Eina_File *eina_file_dup(const Eina_File *file);
EAPI void eina_file_close(Eina_File *file);
EAPI size_t eina_file_size_get(const Eina_File *file);
EAPI time_t eina_file_mtime_get(const Eina_File *file);
EAPI const char *eina_file_filename_get(const Eina_File *file);
EAPI Eina_Iterator *eina_file_xattr_get(Eina_File *file);
EAPI Eina_Iterator *eina_file_xattr_value_get(Eina_File *file);
EAPI void *eina_file_map_all(Eina_File *file, Eina_File_Populate rule);
EAPI void *eina_file_map_new(Eina_File *file, Eina_File_Populate rule,
unsigned long int offset, unsigned long int length);
EAPI void eina_file_map_free(Eina_File *file, void *map);
EAPI void eina_file_map_populate(Eina_File *file, Eina_File_Populate rule,
const void *map, unsigned long int offset, unsigned long int length);
EAPI Eina_Iterator *eina_file_map_lines(Eina_File *file);
EAPI Eina_Bool eina_file_map_faulted(Eina_File *file, void *map);
void eina_stringshare_del (const char *str);
int eina_stringshare_strlen(const char *str);
extern int errno;
char *strerror(int);
void free(void*);
]]
local cutil = require("cutil")
local util = require("util")
local Object = util.Object
local M = {}
local eina
local init = function()
eina = util.lib_load("eina")
end
local shutdown = function()
util.lib_unload("eina")
end
cutil.init_module(init, shutdown)
M.dir_list = function(dir, recursive, cb)
if not cb or not dir then return false end
local cbp = ffi.cast("Eina_File_Dir_List_Cb", function(name, path, data)
return cb(name, path)
end)
local v = eina.eina_file_dir_list(dir, recursive or false, cbp, nil)
cbp:free()
return v == 1
end
local Iterator = iterator.Iterator
M.Ls_Iterator = Iterator:clone {
__ctor = function(self, dir)
return Iterator.__ctor(self, eina.eina_file_ls(dir))
end,
next = function(self)
local v = Iterator.next(self)
if not v then return nil end
local r = ffi.string(v, eina.eina_stringshare_strlen(v))
eina.eina_stringshare_del(v)
return r
end
}
M.ls = M.Ls_Iterator
local file_type_map = {
[C.EINA_FILE_UNKNOWN] = "unknown",
[C.EINA_FILE_FIFO] = "fifo",
[C.EINA_FILE_CHR] = "chr",
[C.EINA_FILE_DIR] = "dir",
[C.EINA_FILE_BLK] = "blk",
[C.EINA_FILE_REG] = "reg",
[C.EINA_FILE_LNK] = "lnk",
[C.EINA_FILE_SOCK] = "sock",
[C.EINA_FILE_WHT] = "wht",
}
local Direct_Info = Object:clone {
__ctor = function(self, path, name_start, name_length, tp)
self.path = path
self.name_start = name_start
self.name_length = name_length
self.type = tp
end,
statat = function(self, container)
if not container then return false, "invalid container" end
local info = ffi.new("Eina_File_Direct_Info", #self.path,
self.name_length, self.name_start - 1,
C["EINA_FILE_" .. self.type:upper()], self.path)
local buf = ffi.new("Eina_Stat")
if eina.eina_file_statat(container, info, buf) ~= 0 then
return false, ffi.string(C.strerror(C.errno))
end
return buf
end
}
local direct_info_iterator_next = function(self)
local v = Iterator.next(self)
if not v then return nil end
local s = ffi.cast("Eina_File_Direct_Info*", v)
local path = ffi.string(s.path, s.path_length)
local ns = tonumber(s.name_start)
local nl = tonumber(s.name_length)
local tp = file_type_map[s.type]
return Direct_Info(path, ns, nl, tp), self:container_get()
end
M.Stat_Ls_Iterator = Iterator:clone {
__ctor = function(self, dir)
return Iterator.__ctor(self, eina.eina_file_stat_ls(dir))
end,
next = direct_info_iterator_next
}
M.stat_ls = M.Stat_Ls_Iterator
M.Direct_Ls_Iterator = Iterator:clone {
__ctor = function(self, dir)
return Iterator.__ctor(self, eina.eina_file_direct_ls(dir))
end,
next = direct_info_iterator_next
}
M.direct_ls = M.Direct_Ls_Iterator
M.path_sanitize = function(path)
local v = eina.eina_file_path_sanitize(path)
if v == nil then return nil end
local r = ffi.string(v)
C.free(v)
return r
end
M.copy_flags = {
DATA = 0, PERMISION = 1, XATTR = 2
}
M.copy = function(source, destination, flags, cb)
if not source or not destination then return false end
flags = flags or 0
local cbp
if cb then
cbp = ffi.cast("Eina_File_Copy_Progress", function(data, done, total)
return not not cb(done, total)
end)
end
local v = eina.eina_file_copy(source, destination, flags, cbp, nil)
if cbp then cbp:free() end
return v == 1
end
M.Xattr_Iterator = Iterator:clone {
__ctor = function(self, file)
return Iterator.__ctor(self, eina.eina_file_xattr_get(file))
end,
next = function(self)
local v = Iterator.next(self)
if not v then return nil end
return ffi.string(v)
end
}
M.Xattr_Value_Iterator = Iterator:clone {
__ctor = function(self, file)
return Iterator.__ctor(self, eina.eina_file_xattr_value_get(file))
end,
next = function(self)
local v = Iterator.next(self)
if not v then return nil end
v = ffi.cast(v, "Eina_Xattr*")
return ffi.string(v.name), ffi.string(v.value, v.length)
end
}
M.populate = {
RANDOM = 0,
SEQUENTIAL = 1,
WILLNEED = 2,
POPULATE = 3,
DONTNEED = 4,
REMOVE = 5
}
M.Line_Iterator = Iterator:clone {
__ctor = function(self, file)
return Iterator.__ctor(self, eina.eina_file_map_lines(file))
end,
next = function(self)
local v = Iterator.next(self)
if not v then return nil end
v = ffi.cast(v, "Eina_File_Line*")
return ffi.string(v.start, v.length), tonumber(v.index)
end
}
M.File = ffi.metatype("Eina_File", {
__new = function(self, name, shared)
return self.open(name, shared)
end,
__len = function(self)
return self:size_get()
end,
__index = {
open = function(name, shared)
return eina.eina_file_open(name, shared)
end,
virtualize = function(vname, data, length, copy)
return eina.eina_file_virtualize(vname, data, length,
copy or false)
end,
close = function(self)
return eina.eina_file_close(self)
end,
dup = function(self)
return eina.eina_file_dup(self)
end,
is_virtual = function(self)
return eina.eina_file_virtual(self) == 1
end,
refresh = function(self)
return eina.eina_file_refresh(self) == 1
end,
size_get = function(self)
return tonumber(eina.eina_file_size_get(self))
end,
mtime_get = function(self)
return tonumber(eina.eina_file_mtime_get(self))
end,
filename_get = function(self)
return ffi.string(eina.eina_file_filename_get(self))
end,
xattr_get = M.Xattr_Iterator,
xattr_value_get = M.Xattr_Value_Iterator,
map_all = function(self, rule, raw)
local v = ffi.cast("char*", eina.eina_file_map_all(self, rule or 0))
if v == nil then return nil end
if not raw then
local r = ffi.string(v)
self:map_free(v)
return r
end
return v
end,
map_new = function(self, rule, offset, length, raw)
local v = ffi.cast("char*", eina.eina_file_map_new(self, rule or 0,
offset or 0, length))
if v == nil then return nil end
if not raw then
local r = ffi.string(v, length)
self:map_free(v)
return r
end
return v
end,
map_free = function(self, map)
return eina.eina_file_map_free(self, map)
end,
map_populate = function(self, rule, map, offset, length)
return eina.eina_file_map_populate(self, rule or 0, offset or 0,
length)
end,
map_faulted = function(self, map)
return eina.eina_file_map_faulted(self, map) == 1
end,
lines = M.Line_Iterator
}
})
return M
|