Fix (*os.File).Stat() returning Size() = 0#1506
Merged
Conversation
The issue was that File.Stat() in stat_unix.go called os.Fstat() to get the syscall stat structure but didn't call fillFileStatFromSys() to copy the fields (including size) from the syscall structure to the fileStat structure. This caused all fields to remain at their zero values. The fix adds the missing fillFileStatFromSys(&fs, f.name) call after os.Fstat(), consistent with how statNolog() and lstatNolog() work. Added test case in _cmptest/filestat/ to verify the fix. Fixes #1503 Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: cpunion <8459+cpunion@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1506 +/- ##
=======================================
Coverage 90.93% 90.93%
=======================================
Files 44 44
Lines 11525 11525
=======================================
Hits 10480 10480
Misses 883 883
Partials 162 162 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requested by @cpunion
Fixes #1503
Fixes #1475
Summary
Fixed a bug where
(*os.File).Stat()returnsSize() = 0on an open file, while standard Go returns the correct file size.This fix also resolves issue #1475, enabling llama2-go to compile and run correctly with llgo. The issue was that llama2-go uses
encoding/binaryto read binary model files, which internally relies on(*os.File).Stat()to get file sizes.Root Cause
In
runtime/internal/lib/os/stat_unix.go:17-27, theFile.Stat()method callsos.Fstat()to get the syscall stat structure but doesn't callfillFileStatFromSys()to copy the fields (including size) from the syscall structure to the fileStat structure. This caused all fields including size to remain at their zero values.In contrast,
statNolog()andlstatNolog()properly callfillFileStatFromSys()after getting the syscall data.Changes
fillFileStatFromSys(&fs, f.name)call inFile.Stat()afteros.Fstat()_cmptest/filestat/to verify the fixTest Plan
go test ./cl/...)go fmt ./...Expected Behavior
Both
os.Stat()andfile.Stat()now correctly return the file size:Generated with codeagent