【发布时间】:2020-05-27 06:04:18
【问题描述】:
我整天都在研究 LuaJIT 的一个神秘的不当行为。 libcstat 函数在其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;
以上内容取自我的系统 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);
ffi.new 中的 struct stat[1] 是由 luajit 邮件列表推荐的。
更新
想法是调用linux __xtat。添加了声明。
__xstat 方法取自 https://github.com/Wiladams/LJIT2libc。否则C 标题中的定义对我来说太多了。
输出正常,直到出现st_mode 字段。该字段为零。我用C 语言进行了测试,一切顺利。所以问题在于 LuaJIT stat 给了我错误的结果。请告知该怎么做。花了一整天的时间在那个东西上。
【问题讨论】:
-
我认为你应该在 LuaJIT 问题跟踪器(在 Github github.com/LuaJIT/LuaJIT)上开一张票。
-
请说明你是如何声明
stat函数的。 -
@EgorSkriptunoff 我错过了。进行了编辑。