我想要记录指定目录下的每个系统调用,并且我找到了这个存储库https://github.com/rflament/loggedfs
它使用fuse创建一个虚拟文件系统,并记录其中的所有内容,就像我想要的那样。
我试图将它移植到mac上,但它使用了一个在osx上不起作用的“技巧”。lstat卡住了10秒并崩溃。
我想知道为什么?
这是我代码的主要部分:
// g++ -Wall main.cpp `pkg-config fuse --cflags --libs` -o hello
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
static char *path;
static int savefd;
static int getattr(const char *path, struct stat *stbuf)
{
int res;
char rPath[1024];
strcpy(rPath, "."); strcat(rPath, path);
res = lstat(rPath, stbuf); // Mac stuck here
return (res == -1 ? -errno : 0);
}
static void* loggedFS_init(struct fuse_conn_info* info)
{
fchdir(savefd); close(savefd); return NULL;
}
int main(int argc, char *argv[])
{
struct fuse_operations oper;
bzero(&oper, sizeof(fuse_operations));
oper.init = loggedFS_init;
oper.getattr = getattr;
path = strdup(argv[argc - 1]);
printf("chdir to %s\n", path);
chdir(path);
savefd = open(".", 0);
return fuse_main(argc, argv, &oper, NULL);
}发布于 2018-01-20 03:41:45
我非常仔细地研究了LoggedFS,并使用pjdfstest测试了它的POSIX遵从性,从而产生了3 issues (or groups of issues)。我最终选择了re-implementing it in Python,完全兼容POSIX。我还没有在OS上测试它,所以我很乐意收到一些反馈;)
你提到的“诀窍”可能是你问题的根源,尽管我不完全确定。它会在路径中添加另一个字符,从而导致一个基本问题,当path的长度接近PATH_MAX时,会导致问题。libfuse已经将带有前导/的路径传递到FUSE操作中。额外的.加上“误导性的”/ (挂载的文件系统的根目录,而不是“全局”根文件夹)是两个字符“太多”,有效地将允许的最大路径长度减少到PATH_MAX减2。我研究了更改PATH_MAX和通知用户陆地软件关于更小的PATH_MAX的选项,哪个turned out to be impossible。
然而,有一种方法可以绕过。不要在init例程中关闭文件描述符savefd。将其保持打开状态,而不是在destroy routine中将其关闭,FUSE将在卸载文件系统时调用它。实际上,您可以使用savefd来指定相对于它的路径。然后,您可以使用fstatat (Linux,OS X / BSD)代替lstat。它的原型看起来像这样:
int fstatat(int dirfd, const char *pathname, struct stat *buf,
int flags);您必须将savefd传递到dirfd中,并在将其传递到pathname之前从path的内容中删除前导/。
https://stackoverflow.com/questions/41941630
复制相似问题