forked from spacewander/luafilesystem
-
Notifications
You must be signed in to change notification settings - Fork 8
/
lfs_ffi.lua
1118 lines (1033 loc) · 32.7 KB
/
lfs_ffi.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local bit = require "bit"
local ffi = require "ffi"
local band = bit.band
local rshift = bit.rshift
local lib = ffi.C
local ffi_str = ffi.string
local concat = table.concat
local has_table_new, new_tab = pcall(require, "table.new")
if not has_table_new or type(new_tab) ~= "function" then
new_tab = function () return {} end
end
local _M = {
_VERSION = "0.1",
}
-- common utils/constants
local IS_64_BIT = ffi.abi('64bit')
local ERANGE = 'Result too large'
ffi.cdef([[
char* strerror(int errnum);
]])
local function errno()
return ffi_str(lib.strerror(ffi.errno()))
end
local OS = ffi.os
-- sys/syslimits.h
local MAXPATH
local wchar_t
if OS == 'Windows' then
MAXPATH = 260
ffi.cdef([[
typedef int mbstate_t;
/*
In VC2015, M$ change the definition of mbstate_t to this and breaks the ABI.
*/
typedef struct _Mbstatet
{ // state of a multibyte translation
unsigned long _Wchar;
unsigned short _Byte, _State;
} _Mbstatet;
typedef _Mbstatet mbstate_t;
size_t mbrtowc(wchar_t* pwc,
const char* s,
size_t n,
mbstate_t* ps);
]])
function wchar_t(s)
local mbstate = ffi.new('mbstate_t[1]')
local wcs = ffi.new('wchar_t[?]', #s + 1)
local i = 0
local offset = 0
local len = #s
while true do
local processed = lib.mbrtowc(
wcs + i, ffi.cast('const char *', s) + offset, len, mbstate)
if processed <= 0 then break end
i = i + 1
offset = offset + processed
len = len - processed
end
return wcs
end
elseif OS == 'Linux' then
MAXPATH = 4096
else
MAXPATH = 1024
end
-- misc
if OS == "Windows" then
local utime_def
if IS_64_BIT then
utime_def = [[
typedef __int64 time_t;
struct __utimebuf64 {
time_t actime;
time_t modtime;
};
typedef struct __utimebuf64 utimebuf;
int _utime64(unsigned char *file, utimebuf *times);
]]
else
utime_def = [[
typedef __int32 time_t;
struct __utimebuf32 {
time_t actime;
time_t modtime;
};
typedef struct __utimebuf32 utimebuf;
int _utime632(unsigned char *file, utimebuf *times);
]]
end
ffi.cdef([[
char *_getcwd(char *buf, size_t size);
int _chdir(const char *path);
int _rmdir(const char *pathname);
int _mkdir(const char *pathname);
]] .. utime_def .. [[
typedef wchar_t* LPTSTR;
typedef unsigned char BOOLEAN;
typedef unsigned long DWORD;
BOOLEAN CreateSymbolicLinkW(
LPTSTR lpSymlinkFileName,
LPTSTR lpTargetFileName,
DWORD dwFlags
);
int _fileno(struct FILE *stream);
int _setmode(int fd, int mode);
]])
function _M.chdir(path)
if type(path) ~= 'string' then
error('path should be a string')
end
if lib._chdir(path) == 0 then
return true
end
return nil, errno()
end
function _M.currentdir()
local size = MAXPATH
while true do
local buf = ffi.new("char[?]", size)
if lib._getcwd(buf, size) ~= nil then
return ffi_str(buf)
end
local err = errno()
if err ~= ERANGE then
return nil, err
end
size = size * 2
end
end
function _M.mkdir(path)
if type(path) ~= 'string' then
error('path should be a string')
end
if lib._mkdir(path) == 0 then
return true
end
return nil, errno()
end
function _M.rmdir(path)
if type(path) ~= 'string' then
error('path should be a string')
end
if lib._rmdir(path) == 0 then
return true
end
return nil, errno()
end
function _M.touch(path, actime, modtime)
local buf
if type(actime) == "number" then
modtime = modtime or actime
buf = ffi.new("utimebuf")
buf.actime = actime
buf.modtime = modtime
end
local p = ffi.new("unsigned char[?]", #path + 1)
ffi.copy(p, path)
local utime = IS_64_BIT and lib._utime64 or lib._utime32
if utime(p, buf) == 0 then
return true
end
return nil, errno()
end
function _M.setmode(file, mode)
if io.type(file) ~= 'file' then
error("setmode: invalid file")
end
if mode ~= nil and (mode ~= 'text' and mode ~= 'binary') then
error('setmode: invalid mode')
end
mode = (mode == 'text') and 0x4000 or 0x8000
local prev_mode = lib._setmode(lib._fileno(file), mode)
if prev_mode == -1 then
return nil, errno()
end
return true, (prev_mode == 0x4000) and 'text' or 'binary'
end
local function check_is_dir(path)
return _M.attributes(path, 'mode') == 'directory' and 1 or 0
end
function _M.link(old, new)
local is_dir = check_is_dir(old)
if lib.CreateSymbolicLinkW(
wchar_t(new),
wchar_t(old), is_dir) ~= 0 then
return true
end
return nil, errno()
end
local findfirst
local findnext
if IS_64_BIT then
ffi.cdef([[
typedef struct _finddata64_t {
uint64_t attrib;
uint64_t time_create;
uint64_t time_access;
uint64_t time_write;
uint64_t size;
char name[]] .. MAXPATH ..[[];
} _finddata_t;
int _findfirst64(const char *filespec, _finddata_t *fileinfo);
int _findnext64(int handle, _finddata_t *fileinfo);
int _findclose(int handle);
]])
findfirst = lib._findfirst64
findnext = lib._findnext64
else
ffi.cdef([[
typedef struct _finddata32_t {
uint32_t attrib;
uint32_t time_create;
uint32_t time_access;
uint32_t time_write;
uint32_t size;
char name[]] .. MAXPATH ..[[];
} _finddata_t;
int _findfirst32(const char* filespec, _finddata_t* fileinfo);
int _findnext32(int handle, _finddata_t *fileinfo);
int _findclose(int handle);
]])
findfirst = lib._findfirst32
findnext = lib._findnext32
end
local function findclose(dentry)
if dentry and dentry.handle ~= -1 then
lib._findclose(dentry.handle)
dentry.handle = -1
end
end
local dir_type = ffi.metatype("struct {int handle;}", {
__gc = findclose
})
local function close(dir)
findclose(dir._dentry)
dir.closed = true
end
local function iterator(dir)
if dir.closed ~= false then error("closed directory") end
local entry = ffi.new("_finddata_t")
if not dir._dentry then
dir._dentry = ffi.new(dir_type)
dir._dentry.handle = findfirst(dir._pattern, entry)
if dir._dentry.handle == -1 then
dir.closed = true
return nil, errno()
end
return ffi_str(entry.name)
end
if findnext(dir._dentry.handle, entry) == 0 then
return ffi_str(entry.name)
end
close(dir)
return nil
end
local dirmeta = {__index = {
next = iterator,
close = close,
}}
function _M.dir(path)
if #path > MAXPATH - 2 then
error('path too long: ' .. path)
end
local dir_obj = setmetatable({
_pattern = path..'/*',
closed = false,
}, dirmeta)
return iterator, dir_obj
end
ffi.cdef([[
int _fileno(struct FILE *stream);
int fseek(struct FILE *stream, long offset, int origin);
long ftell(struct FILE *stream);
int _locking(int fd, int mode, long nbytes);
]])
local mode_ltype_map = {
r = 2, -- LK_NBLCK
w = 2, -- LK_NBLCK
u = 0, -- LK_UNLCK
}
local SEEK_SET = 0
local SEEK_END = 2
local function lock(fh, mode, start, len)
local lkmode = mode_ltype_map[mode]
if not len or len <= 0 then
if lib.fseek(fh, 0, SEEK_END) ~= 0 then
return nil, errno()
end
len = lib.ftell(fh)
end
if not start or start <= 0 then
start = 0
end
if lib.fseek(fh, start, SEEK_SET) ~= 0 then
return nil, errno()
end
local fd = lib._fileno(fh)
if lib._locking(fd, lkmode, len) == -1 then
return nil, errno()
end
return true
end
function _M.lock(filehandle, mode, start, length)
if mode ~= 'r' and mode ~= 'w' then
error("lock: invalid mode")
end
if io.type(filehandle) ~= 'file' then
error("lock: invalid file")
end
local ok, err = lock(filehandle, mode, start, length)
if not ok then
return nil, err
end
return true
end
function _M.unlock(filehandle, start, length)
if io.type(filehandle) ~= 'file' then
error("unlock: invalid file")
end
local ok, err = lock(filehandle, 'u', start, length)
if not ok then
return nil, err
end
return true
end
else
ffi.cdef([[
char *getcwd(char *buf, size_t size);
int chdir(const char *path);
int rmdir(const char *pathname);
typedef unsigned int mode_t;
int mkdir(const char *pathname, mode_t mode);
typedef size_t time_t;
struct utimebuf {
time_t actime;
time_t modtime;
};
int utime(const char *file, const struct utimebuf *times);
int link(const char *oldpath, const char *newpath);
int symlink(const char *oldpath, const char *newpath);
]])
function _M.chdir(path)
if lib.chdir(path) == 0 then
return true
end
return nil, errno()
end
function _M.currentdir()
local size = MAXPATH
while true do
local buf = ffi.new("char[?]", size)
if lib.getcwd(buf, size) ~= nil then
return ffi_str(buf)
end
local err = errno()
if err ~= ERANGE then
return nil, err
end
size = size * 2
end
end
function _M.mkdir(path, mode)
if lib.mkdir(path, mode or 509) == 0 then
return true
end
return nil, errno()
end
function _M.rmdir(path)
if lib.rmdir(path) == 0 then
return true
end
return nil, errno()
end
function _M.touch(path, actime, modtime)
local buf
if type(actime) == "number" then
modtime = modtime or actime
buf = ffi.new("struct utimebuf")
buf.actime = actime
buf.modtime = modtime
end
local p = ffi.new("unsigned char[?]", #path + 1)
ffi.copy(p, path)
if lib.utime(p, buf) == 0 then
return true
end
return nil, errno()
end
function _M.setmode()
return true, "binary"
end
function _M.link(old, new, symlink)
local f = symlink and lib.symlink or lib.link
if f(old, new) == 0 then
return true
end
return nil, errno()
end
local dirent_def
if OS == 'OSX' or OS == 'BSD' then
dirent_def = [[
/* _DARWIN_FEATURE_64_BIT_INODE is NOT defined here? */
struct dirent {
uint32_t d_ino;
uint16_t d_reclen;
uint8_t d_type;
uint8_t d_namlen;
char d_name[256];
};
]]
else
dirent_def = [[
struct dirent {
int64_t d_ino;
size_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};
]]
end
ffi.cdef(dirent_def .. [[
typedef struct __dirstream DIR;
DIR *opendir(const char *name);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
]])
local function close(dir)
if dir._dentry ~= nil then
lib.closedir(dir._dentry)
dir._dentry = nil
dir.closed = true
end
end
local function iterator(dir)
if dir.closed ~= false then error("closed directory") end
local entry = lib.readdir(dir._dentry)
if entry ~= nil then
return ffi_str(entry.d_name)
else
close(dir)
return nil
end
end
local dir_obj_type = ffi.metatype([[
struct {
DIR *_dentry;
bool closed;
}
]],
{__index = {
next = iterator,
close = close,
}, __gc = close
})
function _M.dir(path)
local dentry = lib.opendir(path)
if dentry == nil then
error("cannot open "..path.." : "..errno())
end
local dir_obj = ffi.new(dir_obj_type)
dir_obj._dentry = dentry
dir_obj.closed = false;
return iterator, dir_obj
end
local SEEK_SET = 0
local F_SETLK = (OS == 'Linux') and 6 or 8
local mode_ltype_map
local flock_def
if OS == 'Linux' then
flock_def = [[
struct flock {
short int l_type;
short int l_whence;
int64_t l_start;
int64_t l_len;
int l_pid;
};
]]
mode_ltype_map = {
r = 0, -- F_RDLCK
w = 1, -- F_WRLCK
u = 2, -- F_UNLCK
}
else
flock_def = [[
struct flock {
int64_t l_start;
int64_t l_len;
int32_t l_pid;
short l_type;
short l_whence;
};
]]
mode_ltype_map = {
r = 1, -- F_RDLCK
u = 2, -- F_UNLCK
w = 3, -- F_WRLCK
}
end
ffi.cdef(flock_def..[[
int fileno(struct FILE *stream);
int fcntl(int fd, int cmd, ... /* arg */ );
int unlink(const char *path);
]])
local function lock(fd, mode, start, len)
local flock = ffi.new('struct flock')
flock.l_type = mode_ltype_map[mode]
flock.l_whence = SEEK_SET
flock.l_start = start or 0
flock.l_len = len or 0
if lib.fcntl(fd, F_SETLK, flock) == -1 then
return nil, errno()
end
return true
end
function _M.lock(filehandle, mode, start, length)
if mode ~= 'r' and mode ~= 'w' then
error("lock: invalid mode")
end
if io.type(filehandle) ~= 'file' then
error("lock: invalid file")
end
local fd = lib.fileno(filehandle)
local ok, err = lock(fd, mode, start, length)
if not ok then
return nil, err
end
return true
end
function _M.unlock(filehandle, start, length)
if io.type(filehandle) ~= 'file' then
error("unlock: invalid file")
end
local fd = lib.fileno(filehandle)
local ok, err = lock(fd, 'u', start, length)
if not ok then
return nil, err
end
return true
end
end
-- lock related
local dir_lock_struct
local create_lockfile
local delete_lockfile
if OS == 'Windows' then
ffi.cdef([[
typedef const wchar_t* LPCWSTR;
typedef struct _SECURITY_ATTRIBUTES {
DWORD nLength;
void *lpSecurityDescriptor;
int bInheritHandle;
} SECURITY_ATTRIBUTES;
typedef SECURITY_ATTRIBUTES *LPSECURITY_ATTRIBUTES;
void *CreateFileW(
LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
void *hTemplateFile
);
int CloseHandle(void *hObject);
]])
local GENERIC_WRITE = 0x40000000
local CREATE_NEW = 1
local FILE_NORMAL_DELETE_ON_CLOSE = 0x04000080
dir_lock_struct = 'struct {void *lockname;}'
function create_lockfile(dir_lock, _, lockname)
lockname = wchar_t(lockname)
dir_lock.lockname = lib.CreateFileW(lockname, GENERIC_WRITE, 0, nil, CREATE_NEW,
FILE_NORMAL_DELETE_ON_CLOSE, nil)
return dir_lock.lockname ~= ffi.cast('void*', -1)
end
function delete_lockfile(dir_lock)
return lib.CloseHandle(dir_lock.lockname)
end
else
dir_lock_struct = 'struct {char *lockname;}'
function create_lockfile(dir_lock, path, lockname)
dir_lock.lockname = ffi.new('char[?]', #lockname + 1)
ffi.copy(dir_lock.lockname, lockname)
return lib.symlink(path, lockname) == 0
end
function delete_lockfile(dir_lock)
return lib.unlink(dir_lock.lockname)
end
end
local function unlock_dir(dir_lock)
if dir_lock.lockname ~= nil then
dir_lock:delete_lockfile()
dir_lock.lockname = nil
end
return true
end
local dir_lock_type = ffi.metatype(dir_lock_struct,
{__gc = unlock_dir,
__index = {
free = unlock_dir,
create_lockfile = create_lockfile,
delete_lockfile = delete_lockfile,
}}
)
function _M.lock_dir(path, _)
-- It's interesting that the lock_dir from vanilla lfs just ignores second paramter.
-- So, I follow this behavior too :)
local dir_lock = ffi.new(dir_lock_type)
local lockname = path .. '/lockfile.lfs'
if not dir_lock:create_lockfile(path, lockname) then
return nil, errno()
end
return dir_lock
end
-- stat related
local stat_func
local lstat_func
if OS == 'Linux' then
ffi.cdef([[
long syscall(int number, ...);
]])
local ARCH = ffi.arch
-- Taken from justincormack/ljsyscall
local stat_syscall_num
local lstat_syscall_num
if ARCH == 'x64' then
ffi.cdef([[
typedef struct {
unsigned long st_dev;
unsigned long st_ino;
unsigned long st_nlink;
unsigned int st_mode;
unsigned int st_uid;
unsigned int st_gid;
unsigned int __pad0;
unsigned long st_rdev;
long st_size;
long st_blksize;
long st_blocks;
unsigned long st_atime;
unsigned long st_atime_nsec;
unsigned long st_mtime;
unsigned long st_mtime_nsec;
unsigned long st_ctime;
unsigned long st_ctime_nsec;
long __unused[3];
} stat;
]])
stat_syscall_num = 4
lstat_syscall_num = 6
elseif ARCH == 'x86' then
ffi.cdef([[
typedef struct {
unsigned long long st_dev;
unsigned char __pad0[4];
unsigned long __st_ino;
unsigned int st_mode;
unsigned int st_nlink;
unsigned long st_uid;
unsigned long st_gid;
unsigned long long st_rdev;
unsigned char __pad3[4];
long long st_size;
unsigned long st_blksize;
unsigned long long st_blocks;
unsigned long st_atime;
unsigned long st_atime_nsec;
unsigned long st_mtime;
unsigned int st_mtime_nsec;
unsigned long st_ctime;
unsigned long st_ctime_nsec;
unsigned long long st_ino;
} stat;
]])
stat_syscall_num = IS_64_BIT and 106 or 195
lstat_syscall_num = IS_64_BIT and 107 or 196
elseif ARCH == 's390x' then
ffi.cdef([[
typedef struct {
unsigned long st_dev;
unsigned long st_ino;
unsigned long st_nlink;
unsigned int st_mode;
unsigned int st_uid;
unsigned int st_gid;
int __pad1;
unsigned long st_rdev;
long st_size;
long st_atime;
long st_atime_nsec;
long st_mtime;
long st_mtime_nsec;
long st_ctime;
long st_ctime_nsec;
long st_blksize;
long st_blocks;
long __unused[3];
} stat;
]])
stat_syscall_num = 106
lstat_syscall_num = 107
elseif ARCH == 'arm' then
if IS_64_BIT then
ffi.cdef([[
typedef struct {
unsigned long st_dev;
unsigned long st_ino;
unsigned int st_mode;
unsigned int st_nlink;
unsigned int st_uid;
unsigned int st_gid;
unsigned long st_rdev;
unsigned long __pad1;
long st_size;
int st_blksize;
int __pad2;
long st_blocks;
long st_atime;
unsigned long st_atime_nsec;
long st_mtime;
unsigned long st_mtime_nsec;
long st_ctime;
unsigned long st_ctime_nsec;
unsigned int __unused4;
unsigned int __unused5;
} stat;
]])
stat_syscall_num = 106
lstat_syscall_num = 107
else
ffi.cdef([[
typedef struct {
unsigned long long st_dev;
unsigned char __pad0[4];
unsigned long __st_ino;
unsigned int st_mode;
unsigned int st_nlink;
unsigned long st_uid;
unsigned long st_gid;
unsigned long long st_rdev;
unsigned char __pad3[4];
long long st_size;
unsigned long st_blksize;
unsigned long long st_blocks;
unsigned long st_atime;
unsigned long st_atime_nsec;
unsigned long st_mtime;
unsigned int st_mtime_nsec;
unsigned long st_ctime;
unsigned long st_ctime_nsec;
unsigned long long st_ino;
} stat;
]])
stat_syscall_num = 195
lstat_syscall_num = 196
end
elseif ARCH == 'ppc64le' then
ffi.cdef([[
typedef struct {
unsigned long st_dev;
unsigned long st_ino;
unsigned long st_nlink;
unsigned int st_mode;
unsigned int st_uid;
unsigned int st_gid;
int __pad2;
unsigned long st_rdev;
long st_size;
long st_blksize;
long st_blocks;
long st_atime;
long st_atime_nsec;
long st_mtime;
long st_mtime_nsec;
long st_ctime;
long st_ctime_nsec;
unsigned long __unused4;
unsigned long __unused5;
unsigned long __unused6;
} stat;
]])
stat_syscall_num = 106
lstat_syscall_num = 107
elseif ARCH == 'ppc' or ARCH == 'ppcspe' then
ffi.cdef([[
typedef struct {
unsigned long long st_dev;
unsigned long long st_ino;
unsigned int st_mode;
unsigned int st_nlink;
unsigned int st_uid;
unsigned int st_gid;
unsigned long long st_rdev;
unsigned long long __pad1;
long long st_size;
int st_blksize;
int __pad2;
long long st_blocks;
int st_atime;
unsigned int st_atime_nsec;
int st_mtime;
unsigned int st_mtime_nsec;
int st_ctime;
unsigned int st_ctime_nsec;
unsigned int __unused4;
unsigned int __unused5;
} stat;
]])
stat_syscall_num = IS_64_BIT and 106 or 195
lstat_syscall_num = IS_64_BIT and 107 or 196
elseif ARCH == 'mips' or ARCH == 'mipsel' then
ffi.cdef([[
typedef struct {
unsigned long st_dev;
unsigned long __st_pad0[3];
unsigned long long st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
unsigned long st_rdev;
unsigned long __st_pad1[3];
long long st_size;
time_t st_atime;
unsigned long st_atime_nsec;
time_t st_mtime;
unsigned long st_mtime_nsec;
time_t st_ctime;
unsigned long st_ctime_nsec;
unsigned long st_blksize;
unsigned long __st_pad2;
long long st_blocks;
long __st_padding4[14];
} stat;
]])
stat_syscall_num = IS_64_BIT and 4106 or 4213
lstat_syscall_num = IS_64_BIT and 4107 or 4214
end
if stat_syscall_num then
stat_func = function(filepath, buf)
return lib.syscall(stat_syscall_num, filepath, buf)
end
lstat_func = function(filepath, buf)
return lib.syscall(lstat_syscall_num, filepath, buf)
end
else
ffi.cdef('typedef struct {} stat;')
stat_func = function() error("TODO support other Linux architectures") end
lstat_func = stat_func
end
elseif OS == 'Windows' then
ffi.cdef([[
typedef struct {
unsigned int st_dev;
unsigned short st_ino;
unsigned short st_mode;
short st_nlink;
short st_uid;
short st_gid;
unsigned int st_rdev;
int64_t st_size;
long long st_atime;
long long st_mtime;
long long st_ctime;
} stat;
int _stat64(const char *path, stat *buffer);
]])
stat_func = lib._stat64
lstat_func = stat_func
elseif OS == 'OSX' then
ffi.cdef([[
struct timespec {
time_t tv_sec;
long tv_nsec;
};
typedef struct {
uint32_t st_dev;
uint16_t st_mode;
uint16_t st_nlink;
uint64_t st_ino;
uint32_t st_uid;
uint32_t st_gid;
uint32_t st_rdev;
struct timespec st_atimespec;
struct timespec st_mtimespec;
struct timespec st_ctimespec;
struct timespec st_birthtimespec;
int64_t st_size;
int64_t st_blocks;
int32_t st_blksize;
uint32_t st_flags;
uint32_t st_gen;
int32_t st_lspare;
int64_t st_qspare[2];
} stat;
int stat64(const char *path, stat *buf);
int lstat64(const char *path, stat *buf);
]])
stat_func = lib.stat64
lstat_func = lib.lstat64
else
ffi.cdef('typedef struct {} stat;')
stat_func = function() error('TODO: support other posix system') end
lstat_func = stat_func
end
local STAT = {
FMT = 0xF000,
FSOCK = 0xC000,
FLNK = 0xA000,
FREG = 0x8000,
FBLK = 0x6000,
FDIR = 0x4000,
FCHR = 0x2000,
FIFO = 0x1000,
}
local ftype_name_map = {
[STAT.FREG] = 'file',
[STAT.FDIR] = 'directory',
[STAT.FLNK] = 'link',
[STAT.FSOCK] = 'socket',
[STAT.FCHR] = 'char device',
[STAT.FBLK] = "block device",
[STAT.FIFO] = "named pipe",
}