Skip to content
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

Add fileTorrentImpl.Flush() method #937

Merged
merged 1 commit into from
Apr 26, 2024
Merged
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
24 changes: 24 additions & 0 deletions storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (fs fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash
return TorrentImpl{
Piece: t.Piece,
Close: t.Close,
Flush: t.Flush,
}, nil
}

Expand Down Expand Up @@ -122,6 +123,29 @@ func (fs *fileTorrentImpl) Close() error {
return nil
}

func fsync(filePath string) (err error) {
_ = os.MkdirAll(filepath.Dir(filePath), 0o777)
var f *os.File
f, err = os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return err
}
defer f.Close()
if err = f.Sync(); err != nil {
return err
}
return f.Close()
}

func (fts *fileTorrentImpl) Flush() error {
for _, f := range fts.files {
if err := fsync(f.path); err != nil {
return err
}
}
return nil
}

// A helper to create zero-length files which won't appear for file-orientated storage since no
// writes will ever occur to them (no torrent data is associated with a zero-length file). The
// caller should make sure the file name provided is safe/sanitized.
Expand Down
Loading