我一整天都在研究 LuaJIT 的一个神秘的不当行为。 libc
stat
函数在其 stat
中返回错误值缓冲区。
LuaJIT 脚本:
-- definitions for sys/types.h
typedef uint32_t mode_t;
typedef uint64_t dev_t;
typedef uint64_t ino_t;
typedef unsigned int nlink_t;
typedef int pid_t;
typedef unsigned int id_t;
typedef unsigned int uid_t;
typedef unsigned int gid_t;
typedef int64_t off_t;
typedef long blksize_t;
typedef int64_t blkcnt_t;
typedef uint64_t fsblkcnt_t;
typedef uint64_t fsfilcnt_t;
-- for sys/stat.h
struct stat {
dev_t st_dev; /* Device */
ino_t st_ino; /* File serial number. */
nlink_t st_nlink; /* Link count. */
mode_t st_mode; /* File mode. */
uid_t st_uid; /* User ID of the file's owner. */
gid_t st_gid; /* Group ID of the file's group.*/
int __pad0;
dev_t st_rdev; /* Device number, if device. */
off_t st_size; /* Size of file, in bytes. */
blksize_t st_blksize; /* Optimal block size for I/O. */
blkcnt_t st_blocks; /* Number 512-byte blocks allocated. */
/* __USE_XOPEN2K8 */
struct timespec st_atim; /* Time of last access. */
struct timespec st_mtim; /* Time of last modification. */
struct timespec st_ctim; /* Time of last status change. */
long __unused[3];
};
/* luajit calls this */
int __xstat(int ver, const char *path, struct stat *buf);
-- lua stat function part
stat = function(path, buf) return ffi.C.__xstat(_STAT_VER, path, buf) end;
以上内容取 self 的系统 C 头文件。现在 LuaJIT 调用是:
local buf = ffi.new("struct stat[1]")
local res = stat('main.c', buf)
ffi.cdef [[
int printf(const char *fmt, ...);
]]
ffi.C.printf("size: %lu, ino: %lu, mode: %d\n", buf[0].st_size, buf[0].st_ino, buf[0].st_mode);
struct stat[1]
在ffi.new
luajit 邮件列表友情建议。
更新
这个想法是调用 linux __xtat。已添加声明。
__xstat
方法取自 https://github.com/Wiladams/LJIT2libc 。否则 C
中的定义太多了给我的标题。
输出正常,直到 st_mode
field 。场为零。我用 C
做了测试语言,一切都很顺利。所以问题出在 LuaJIT stat
给我错误的结果。请告知该怎么做。一整天都在那个东西上。
最佳答案
我在 ffi.cdef
中犯了一些拼写错误类型声明。在luajit
用户的帮助下,问题现已解决。邮件列表。要快速制作简历:
- 输出
clang -E <some_c_file>.c
在C
上调用的命令源文件,包含#include <sys/stat.h>
. - 制作适当的
struct stat
定义。 - 我的系统
stat
函数有太多级别的宏垃圾。最后,stat
函数调用__xstat
。对我来说唯一明智的方式是调用stat
功能是制作一个syscall
. - 请记住
Lua
print
函数不知道cdata
类型。和printf
不知道Lua
类型。但是luajit
关于如何来回转换常见数据类型有一个很好且简单的解释。
关于c - libc stat 函数和 LuaJIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60175500/