Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions embedfs/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ func (f *file) Truncate(_ int64) error {
return billy.ErrReadOnly
}

// Sync for embedfs file is a no-op.
func (f *file) Sync() error {
return nil
}

// Write is not supported.
//
// Calls will always return billy.ErrReadOnly.
Expand Down
2 changes: 2 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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
}

}

// Capable interface can return the available features of a filesystem.
Expand Down
4 changes: 4 additions & 0 deletions internal/test/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (*FileMock) Stat() (fs.FileInfo, error) {
return nil, nil
}

func (*FileMock) Sync() error {
Copy link
Member

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.

return nil
}

func (*FileMock) Truncate(_ int64) error {
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions memfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func (f *file) Unlock() error {
return nil
}

// Sync is a no-op in memfs.
func (f *file) Sync() error {
Copy link
Member

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.

return nil
}

type fileInfo struct {
name string
size int
Expand Down
4 changes: 4 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

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:

Suggested change
err = f.Sync()
if sf, ok := f.(Syncer); ok {
err = f.Sync()

if err != nil {
return err
}

return nil
}
Expand Down