-1
int stat(const char *filename, struct stat *buf);
int fstat(int fd, struct stat *buf);

Do we need to open a file to get its attributes?

To access the attributes of a file, do we need to access the file's entry in file desriptor table, in file table, and/or in vnode table?

Is stat() implemented by first calling open() to get a file descriptor of the given file, and then calling fstat() or something equivalent on the file descriptor?

Thanks.

  • They're both simple C wrapper of syscalls provided by kernel. I don't think one syscall would be implemented using another one, if talking about kernel implementation. – 炸鱼薯条德里克 Mar 5 at 2:38
3

No, you don't need to open it.

The stat() syscall never involves a file descriptor anywhere.

In fact, you may be able to stat() some files that you don't have permission to open().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.