-
Notifications
You must be signed in to change notification settings - Fork 47
osfs: add file.Sync()
support
#126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Adds a call to `file.Sync()` to `util.WriteFile()` (right before closing the file, so it should be functionally equivalent) to ensure test coverage. Fixes: Issue go-git#86
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@0xDEC0DE thanks for looking into this.
@@ -178,6 +178,8 @@ type File interface { | |||
Unlock() error | |||
// Truncate the file. | |||
Truncate(size int64) error | |||
// Commit the current contents of the file to stable storage. | |||
Sync() error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of expanding the existing interface, let's create a new interface for this:
type Syncer interface {
Sync() error
}
@@ -146,6 +146,11 @@ func (f *file) Unlock() error { | |||
return nil | |||
} | |||
|
|||
// Sync is a no-op in memfs. | |||
func (f *file) Sync() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid LSP violations, we don't implement the Sync on any structure that does not really support it.
@@ -116,6 +116,10 @@ func WriteFile(fs billy.Basic, filename string, data []byte, perm fs.FileMode) ( | |||
if err == nil && n < len(data) { | |||
err = io.ErrShortWrite | |||
} | |||
err = f.Sync() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should call Sync for implementations that support it:
err = f.Sync() | |
if sf, ok := f.(Syncer); ok { | |
err = f.Sync() |
@@ -140,6 +140,10 @@ func (*FileMock) Stat() (fs.FileInfo, error) { | |||
return nil, nil | |||
} | |||
|
|||
func (*FileMock) Sync() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test to confirm Sync is being called.
Adds a call to
file.Sync()
toutil.WriteFile()
(right before closing the file, so it should be functionally equivalent) to ensure test coverage.Fixes: Issue #86