PowerColorLS icon indicating copy to clipboard operation
PowerColorLS copied to clipboard

Ignore symlinks when calculating directory sizes

Open rashil2000 opened this issue 5 years ago • 8 comments
trafficstars

I noticed it's showing ~2x the size of a particular folder when compared to the Explorer's Properties dialog. Upon inspecting, I found out that the folder contained symlinks to some other folders.

The --show-directory-sizes flag should ideally ignore symlinked (soft linked) folders and should not recurse into them to add their size to the output.

rashil2000 avatar Nov 19 '20 17:11 rashil2000

I think this seems to be a common problem with PS modules, as I had opened the same issue in this repository too about a month ago - gngrninja/PSFolderSize#20

rashil2000 avatar Nov 19 '20 17:11 rashil2000

If you have time I welcome a contribution to fix this. Otherwise I will look into it when I have the time :)

gardebring avatar Nov 20 '20 14:11 gardebring

Upon some rudimentary research, I found that this line https://github.com/gardebring/PowerColorLS/blob/main/src/Private/FileAndFolderFunctions.ps1#L112 would require some object filtering in between the pipes; excluding directories that have the ReparsePoint attribute (source - https://stackoverflow.com/questions/817794/find-out-whether-a-file-is-a-symbolic-link-in-powershell).

Is this correct? Note that this might require additional checks for figuring out junctions/hardlinks too.

I have my uni exams, I'll definitely try to send a PR once they're done.

rashil2000 avatar Nov 20 '20 17:11 rashil2000

Hi again @rashil2000 You are definitely at the right point in the code. I did however try to create a symlink setup that should create recursion but it seems the powershell code in fact did not do this.

gardebring avatar Nov 22 '20 01:11 gardebring

Yes, it seems like when we specify the ReparsePoint attribute, it just excludes those folders from listing but still recurses into them.

PowerShell 6+ doesn't follow symlinks by default, but provides a FollowSymlink flag in case we want to recurse into them.

However, for 5.1, we'll need to create a custom Get-ChildItem function, the one specified in this brilliant answer - https://stackoverflow.com/a/55544084

I tried using this and gives correct results for now. I'll do some more testing and send a PR :)

rashil2000 avatar Nov 29 '20 07:11 rashil2000

@rashil2000 any pr on the horizon?

eabase avatar Feb 06 '22 11:02 eabase

Just keep in mind the difference between hardlinks, junctions, symbolic links, shortcuts, etc. Lot's of things there...

  • https://superuser.com/questions/752538/mklink-vs-junction-exe
  • https://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html
  • https://kimconnect.com/windows-short-cut-vs-symbolic-link-vs-junction-vs-hard-link/
  • https://superuser.com/questions/253935/what-is-the-difference-between-symbolic-link-and-shortcut

eabase avatar Feb 06 '22 11:02 eabase

Not yet. Reason being, I rely on other CLI utilities for disk usage now (for example, gdu in Go or dua in Rust) as PowerShell is just too slow for this work.

However, it has been partly fixed in the issue I linked above, see https://github.com/gngrninja/PSFolderSize/commit/adf71c7ed046f5eb2fdc2028aebb12ddcab666f8.

rashil2000 avatar Feb 06 '22 12:02 rashil2000