4 ms·
You would still have to getdents() everything but this way you may indeed save on stat() operations, which access information that is stored separately on disk
by geertj 2y ago
You would still have to getdents() everything but this way you may indeed save on stat() operations, which access information that is stored separately on disk and eliminating these would likely help uncached runs.
You could sample files in a directory or across directories to get an average file size and use the total number of files from getdents to estimate a total size. This does require you to know if a directory entry is a file or directory, which the d_type field gives you depending on the OS, file system and other factors. An average file size could also be obtained from statvfs().
Another trick is based on the fact that the link count of a directory is 2 + the number of subdirectories. Once you have seen the corresponding number of subdirectories, you know that there are no more subdirectories you need to descend into. This could allow you to abort a getdents for a very large directory, using eg the directory size to estimate the total entries.
- olddustytrail 2y ago> Another trick is based on the fact that the link count of a directory is 2 + the number of subdirectories. For anyone who doesn't know why this is, it's because when you create a directory it has 2 hard links to it which are dirname dirname/. When you add a new subdirectory it adds one more link which is dirname/subdir/.. So each subdirectory adds one more to the original 2.