]> Sergey Matveev's repositories - btrtrc.git/commitdiff
file.Flush() (#937)
authorAlex Sharov <AskAlexSharov@gmail.com>
Fri, 26 Apr 2024 06:51:39 +0000 (13:51 +0700)
committerGitHub <noreply@github.com>
Fri, 26 Apr 2024 06:51:39 +0000 (16:51 +1000)
storage/file.go

index d15e579d7e7d92aceb901165180313b46cddeb95..54d1a62b72c706262d65159189ca6dacfb86a5b0 100644 (file)
@@ -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
 }
 
@@ -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.