]> Sergey Matveev's repositories - btrtrc.git/blob - storage/file_storage_piece.go
Avoid race condition in test
[btrtrc.git] / storage / file_storage_piece.go
1 package storage
2
3 import (
4         "io"
5         "os"
6
7         "github.com/anacrolix/torrent/metainfo"
8 )
9
10 type fileStoragePiece struct {
11         *fileTorrentImpl
12         p metainfo.Piece
13         io.WriterAt
14         io.ReaderAt
15 }
16
17 func (me *fileStoragePiece) pieceKey() metainfo.PieceKey {
18         return metainfo.PieceKey{me.infoHash, me.p.Index()}
19 }
20
21 func (fs *fileStoragePiece) GetIsComplete() bool {
22         ret, err := fs.completion.Get(fs.pieceKey())
23         if err != nil || !ret {
24                 return false
25         }
26         // If it's allegedly complete, check that its constituent files have the
27         // necessary length.
28         for _, fi := range extentCompleteRequiredLengths(fs.p.Info, fs.p.Offset(), fs.p.Length()) {
29                 s, err := os.Stat(fs.fileInfoName(fi))
30                 if err != nil || s.Size() < fi.Length {
31                         ret = false
32                         break
33                 }
34         }
35         if ret {
36                 return true
37         }
38         // The completion was wrong, fix it.
39         fs.completion.Set(fs.pieceKey(), false)
40         return false
41 }
42
43 func (fs *fileStoragePiece) MarkComplete() error {
44         fs.completion.Set(fs.pieceKey(), true)
45         return nil
46 }
47
48 func (fs *fileStoragePiece) MarkNotComplete() error {
49         fs.completion.Set(fs.pieceKey(), false)
50         return nil
51 }