Question

In many cases I have seen an fstat call performed directly after the file descriptor has been assigned from a call to open:

fd = open(file, flags, mode);
fstat_result = fstat(fd, &stat_log);

Does fstat behave any differently if one has already performed a call to read first?

fd = open(file, flags, mode);
read_result = read(fd, buffer, buffersize)
fstat_result = fstat(fd, &stat_log);

And by "behave differently" I mean: Is the return value of fstat, or anything in the "struct stat" buffer output different in that scenario?

Do any of the size related members of stat_log now represent the size of the file remaining to be read?

If I have already read to the end of the file, will performing a call to fstat thereafter result in a failure? (fstat returning -1)

This fstat document doesn't seem to indicate any need for the file descriptor to reference a file that has not been read from yet. Have you found anything to indicate otherwise?

Was it helpful?

Solution

You can read from a file after opening it but before an fstat. The only thing that will change is any of the attributes that change as a result of that read. The only candidate would be atime, which would be the time of the read, which might be later than the time of the open.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top