Reputation: 2292
Question Solved!
Thanks to @basile-starynkevitch , I find out that the struct stat
is not at the same size in different files!
In <sys/stat.h>
, sizeof(struct stat)
is 88bytes, but with fuse's lib(I guess that due to -D_FILE_OFFSET_BITS=64
flag), this is 96bytes.
So when I add fuse lib to my remote server(add -D_FILE_OFFSET_BITS=64 /usr/local/lib/libfuse.so /usr/local/lib/libulockmgr.so
flag to gcc), then my program runs correctly!
Thanks to your help!
I am doing some projects on fuse, and it got me crazy.
I send struct stat *stbuf 's data from a remote server, and the data is correct in both server and client, but when I use memcpy to duplicate the data into stbuf, it seems that nothing copied. I also try to use read(socked,stbuf,sizeof(struct stat)); directly, but that does not work too.
here is the codes...(if there is no such file, the remote server save -ENOENT in 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;
}
the data I get from 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}
after read(), got data in 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
copy data to 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 does not change at all.
Can anyone give me some suggestion about such phenomenon? I've done some work but still not find a solution.
Upvotes: 2
Views: 2583
Reputation: 1
You don't check the number of read bytes res
in your code.
It can be 0 on end of file, it can be -1 on errors, and it could be less than sizeof(struct stat)
if all bytes have not being received...
Read (with your eyes and brain) very carefully (and read it again twice) the man page of read(2) syscall.
Upvotes: 1