stat weirdness

"Andrew Bachmann" <[email protected]> Thu, 29 Jan 2004 21:35:22 -0800 PST
Newsgroups gmane.os.openbeos.storage
Message-ID <246372163903-BeMail@shuttle>
Hello all,

I found this weirdness while using stat in beos R5, on a bfs partition.  I thought it might be of 
interest to some.

I was downloading a file in beshare.  While doing this I ran stat on the file, in a tight loop.  I 
checked for changing size and changing modification time.  Specifically, I checked to see if the 
size changed without the modification time changing, and vice versa.

I expected to see one of two things.  The first possibility was that they always get updated 
atomically, and one never changes without the other.  The second possibility was that one was 
updated before the other, and I presumed that it would always be the same field updated.

For the most part, the fields are updated together, but occasionally they are not.  When they are 
not the resulting stat has an st_size of zero.  This is despite the fact that the file obviously has 
several megabytes of data.  I think this is a property of R5 bfs implementation, although it could 
be someplace else.  I hope that this property doesn't live in our openbfs implementation. :-)

Here's the test (extra stuff trimmed):

int main() {
  int fd = open("filename", O_RDONLY);
  struct stat st;
  off_t osize;
  time_t otime;
  while (true) {
	osize = st.st_size;
    otime = st.st_mtime;
    fstat(fd, &st);
	if ((osize != st.st_size) && (otime == st.st_mtime) ||
        (osize == st.st_size) && (otime != st.st_mtime)) {
		fprintf(stderr, "mismatch: ");
		fprintf(stderr, "os = %lu, ns = %lu : ", osize, st.st_size);
		fprintf(stderr, "ot = %lu, nt = %lu\n", otime, st.st_mtime);
    }
}

Andrew