If the directory contains symlinks, then dir::get_size follows the symlinks and counts the sizes of those files too. If the targets of those symlinks are inside that same directory structure, then it effectively means the file sizes of some files are counted twice. Or if the targets of those symlinks are not inside that directory structure, then it means the returned size isn't really that of the requested directory, but also includes that of arbitrary other locations on the filesystem, which doesn't seem desirable.
...so 1048576 + 10 (for the symlink) + 128 (for the directory itself) = 1048714
ie something like:
$ cargo run -q
size = 1048714
Actual
fs_extra::dir::get_size() follows the symlink, so counts the size of that file twice. It also doesn't seem to count the size of the directory entry itself.
Summary
If the directory contains symlinks, then
dir::get_size
follows the symlinks and counts the sizes of those files too. If the targets of those symlinks are inside that same directory structure, then it effectively means the file sizes of some files are counted twice. Or if the targets of those symlinks are not inside that directory structure, then it means the returned size isn't really that of the requested directory, but also includes that of arbitrary other locations on the filesystem, which doesn't seem desirable.Steps to reproduce
mkdir test-dir
truncate -s 1M test-dir/large-file
ln -sr test-dir/large-file test-dir/symlink-to-large-file
fs_extra
v1.2.0
):Expected
fs_extra::dir::get_size()
returns the same size asls
would, eg:...so 1048576 + 10 (for the symlink) + 128 (for the directory itself) = 1048714
ie something like:
Actual
fs_extra::dir::get_size()
follows the symlink, so counts the size of that file twice. It also doesn't seem to count the size of the directory entry itself.Notes
I believe the cause is that
fs_extra::dir::get_size()
usesstd::fs::metadata
notstd::fs::symlink_metadata
, and that the former follows symlinks. Also, use ofstd::fs::metadata
is actually unnecessary since thestd::fs::DirEntry
returned bystd::fs::read_dir
already has the metadata we need.