2

问题解决了! 感谢@basile-starynkevitch,我发现struct stat不同文件中的大小不同!

<sys/stat.h>,sizeof(struct stat)是 88 字节,但使用 fuse 的库(我猜是由于-D_FILE_OFFSET_BITS=64标志),这是 96 字节。

因此,当我将 fuse lib 添加到远程服务器(-D_FILE_OFFSET_BITS=64 /usr/local/lib/libfuse.so /usr/local/lib/libulockmgr.so向 gcc 添加标志)时,我的程序运行正常!

感谢您的帮助!


我正在做一些关于保险丝的项目,这让我发疯了。

我从远程服务器发送 struct stat *stbuf 的数据,服务器和客户端的数据都是正确的,但是当我使用 memcpy 将数据复制到 stbuf 时,似乎没有复制任何内容。我也尝试使用 read(socked,stbuf,sizeof(struct stat)); 直接,但这也不起作用。

这是代码......(如果没有这样的文件,远程服务器将 -ENOENT 保存在 st_ino 中)

    static int rof_getattr(const char *path, struct stat *stbuf)

        {

    int res = 0;
    struct cmd sndcmd;
    struct stat buf;

    memset(&sndcmd, 0, sizeof(struct cmd));

    strcpy(sndcmd.cmd, "GETATTR");
    strcpy(sndcmd.str, path);
    memset(stbuf, 0, sizeof(struct stat));
GTTR_AGN:   
    memset(&buf, 0,sizeof(struct stat));
    write(sockfd, &sndcmd, sizeof(struct cmd));
    res=read(sockfd, &buf, sizeof(struct stat));

    if(res!=sizeof(struct stat))
        goto GTTR_AGN;

    memcpy(stbuf,&buf,sizeof(buf));
    if (buf.st_ino==-ENOENT)
        return -ENOENT;
return 0;
        }

我从 gdb 获得的数据:

39      res=read(sockfd, &buf, sizeof(struct stat));
3: (struct stat)stbuf = {st_dev = 694294557525955008, __pad1 = 0, __st_ino = 0, 
  st_mode = 0, st_nlink = 0, st_uid = 0, st_gid = 0, st_rdev = 0, __pad2 = 0, 
  st_size = 0, st_blksize = 0, st_blocks = 0, st_atim = {tv_sec = 0, tv_nsec = 0}, 
  st_mtim = {tv_sec = 0, tv_nsec = 0}, st_ctim = {tv_sec = 0, tv_nsec = 0}, 
  st_ino = 0}
1: buf = {st_dev = 0, __pad1 = 0, __st_ino = 0, st_mode = 0, st_nlink = 0, 
  st_uid = 0, st_gid = 0, st_rdev = 0, __pad2 = 0, st_size = 0, st_blksize = 0, 
  st_blocks = 0, st_atim = {tv_sec = 0, tv_nsec = 0}, st_mtim = {tv_sec = 0, 
    tv_nsec = 0}, st_ctim = {tv_sec = 0, tv_nsec = 0}, st_ino = 0}

read() 后,在 buf 中获取数据

(gdb) s
40      memcpy(stbuf,&buf,sizeof(buf));
3: (struct stat)stbuf = {st_dev = 694294557525955008, __pad1 = 2049, __st_ino = 0, 
  st_mode = 0, st_nlink = 943887, st_uid = 16877, st_gid = 2, 
  st_rdev = 4294967297000, __pad2 = 0, st_size = 0, st_blksize = 4096, 
  st_blocks = 34359742464, st_atim = {tv_sec = 1323833759, tv_nsec = 75415995}, 
  st_mtim = {tv_sec = 1323729515, tv_nsec = 6514929}, st_ctim = {
    tv_sec = 1323729515, tv_nsec = 6514929}, st_ino = 0}
1: buf = {st_dev = 2049, __pad1 = 0, __st_ino = 943887, st_mode = 16877, 
  st_nlink = 2, st_uid = 1000, st_gid = 1000, st_rdev = 0, __pad2 = 0, 
  st_size = 17592186048512, st_blksize = 8, st_blocks = 323909233444133279, 
  st_atim = {tv_sec = 1323729515, tv_nsec = 6514929}, st_mtim = {
    tv_sec = 1323729515, tv_nsec = 6514929}, st_ctim = {tv_sec = 0, tv_nsec = 0}, 
  st_ino = 0}
(gdb) s

将数据复制到 stbuf

41      if (stbuf->st_ino==-ENOENT)
3: (struct stat)stbuf = {st_dev = 694294557525955008, __pad1 = 2049, __st_ino = 0, 
  st_mode = 0, st_nlink = 943887, st_uid = 16877, st_gid = 2, 
  st_rdev = 4294967297000, __pad2 = 0, st_size = 0, st_blksize = 4096, 
  st_blocks = 34359742464, st_atim = {tv_sec = 1323833759, tv_nsec = 75415995}, 
  st_mtim = {tv_sec = 1323729515, tv_nsec = 6514929}, st_ctim = {
    tv_sec = 1323729515, tv_nsec = 6514929}, st_ino = 0}
1: buf = {st_dev = 2049, __pad1 = 0, __st_ino = 943887, st_mode = 16877, 
  st_nlink = 2, st_uid = 1000, st_gid = 1000, st_rdev = 0, __pad2 = 0, 
  st_size = 17592186048512, st_blksize = 8, st_blocks = 323909233444133279, 
  st_atim = {tv_sec = 1323729515, tv_nsec = 6514929}, st_mtim = {
    tv_sec = 1323729515, tv_nsec = 6514929}, st_ctim = {tv_sec = 0, tv_nsec = 0}, 
  st_ino = 0}

stbuf 根本没有改变。

谁能给我一些关于这种现象的建议?我做了一些工作,但仍然没有找到解决方案。

4

1 回答 1

1

您不检查代码中读取的字节数 res

它可以是文件末尾的 0,它可以是错误的 -1,并且它可能小于sizeof(struct stat)所有字节都没有被接收到的情况......

非常仔细地阅读(用你的眼睛和大脑)(并再次阅读两次)read(2)系统调用的手册页。

于 2011-12-14T05:57:25.610 回答