chacham15
chacham15

Reputation: 14261

Why is fstat not equal to stat?

The code is supposed to watch for filesystem changes, but fstat is producing different results than stat, why?


struct stat info;
struct stat finfo;
char *path = "test";
int fd = open(path, O_EVTONLY);
if (fd <= 0){
    exit(-1);
}
wait_for_write(fd);
int err = fstat(fd, &finfo);
if (err){
    exit(-1);
}
err = stat(path, &info);
if (err){
    exit(-1);
}
if (finfo.st_mtimespec.tv_sec != info.st_mtimespec.tv_sec){
    //This happens!
    exit(-1);
}

finfo.st_mtimespec.tv_sec is the value corresponding to the time before the write occured and info.st_mtimespec.tv_sec is the value corresponding to after

Upvotes: 0

Views: 1126

Answers (2)

chacham15
chacham15

Reputation: 14261

O_EVTONLY caches the modified time for successive queries.

Upvotes: 0

wildplasser
wildplasser

Reputation: 44250

You should not use finfo.st_mtimespec.tv_sec, as it is internal to the implementation; use finfo.st_mtime instead. (this is not the answer to your 'problem', but it may avoid related problems in the future)

Upvotes: 1

Related Questions