storage/file.go | 24 ++++++++++++++++++++++++ diff --git a/storage/file.go b/storage/file.go index d15e579d7e7d92aceb901165180313b46cddeb95..54d1a62b72c706262d65159189ca6dacfb86a5b0 100644 --- a/storage/file.go +++ b/storage/file.go @@ -90,6 +90,7 @@ } return TorrentImpl{ Piece: t.Piece, Close: t.Close, + Flush: t.Flush, }, nil } @@ -119,6 +120,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 }