Based on how stat and lstat are patched in Windows, there are a couple of problems:
- Monkey patches should probably execute code lazily, rather than optimistically (if possible) - i.e. don't calculate the lstat type until is actually asked for, then keep using the same value
- ((Windows::Security.get_mode}} is called every time the stats .mode is accessed - i.e. https://github.com/puppetlabs/puppet/blob/master/lib/puppet/util/windows/file.rb#L261
To fix this, first capture the non-Windows behavior in tests. Find out if stat / lstat types are calculated immediately or not (as class methods, I would presume they're asked for then immediately thrown away). Similarly, ensure the behavior of the patched mode is consistent with other platforms.
The goal is to ensure that our file system access patterns are inline with the semantics that these APIs normally impose.