pkg/ls:FromOSFileInfo: fix a panic if fi.Sys() returns the wrong type #2723
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
FromOSFileInfo had a blind cast of a FileInfo.Sys() to *syscall.Stat_t.
This could fail with a badly behaving file system, e.g. a 9p server that would fail a Stat in ways that the go runtime was not prepared for. Ask me how I know :-) The fact that we have not seen this in 11 years of use testifies to its rarity, but ... it can happen.
In FromOSFileInfo, pick default values for UID, GID, and rdev
Check if fi.Sys() is a *syscall.Stat_t and, if so, use Uid, Gid, and Rdev from it.
Add a type to the linux test that implements os.FileInfo (yes, fs.FileInfo now ...) that can return a Sys() of the wrong type. I verified that this test
catches the failing case.
A bit of cleanup:
Rename the test file ls_linux_test.go to fileinfo_linux_test.go, to match everything else in this directory.
Remove a spurious fmt.Printf from the fileinfo_linux_test.go that added trash to the test output.