>> Hello Everyone,
>> I wrote a program that reads the contents of a directory. I am using
>> "dirent.h" to get my file name.
>> I also need to get the file date and, for that, I am using stat(). The
>> problem is that when you use stat(), it reads the entire file system to
>> get to the filename that you have specified and thus it reads each file
>> twice for each call.
>Wrong. It must read the inode for each file. But obviously this takes
>time. From your example (250,000 files in 3 hours) your OS is managing
>about 20+ files per second. Not infinitely fast, but not too shabby.
>More detail: a directory entry contains the file name and the inode
>number. This number maps directly to the inode, which is read in one I/O
>operation. Its direct addressing. No scanning or anything like that. You
>can't get the file's stat() information any faster except by spinning
>the disk faster.
I think his complaint is that readdir() already has a pointer to the
directory entry, but when you call stat() on a filename the kernel has to
scan all the way through the directory to find that entry, in order to get
the inode number. So stat'ing every entry in a directory seems like it's
an N-squared operation.
As I mentioned in my other post, there's a name cache. However, on
reflection, I don't know offhand if names that are returned by readdir()
are put in it. The name might not be cached until you call stat(), by
which time it's too late (what he'd like to do is speed up the stat() in
the first place).
As I mentioned, the best solution is to organize your directory better.
The kernel handles deep, thin directory trees much more efficiently than
shallow, wide trees.
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.