]> Sergey Matveev's repositories - btrtrc.git/blob - storage/file-piece.go
Remove torrentfs-macos job from CI
[btrtrc.git] / storage / file-piece.go
1 package storage
2
3 import (
4         "io"
5         "log"
6         "os"
7
8         "github.com/anacrolix/torrent/metainfo"
9 )
10
11 type filePieceImpl struct {
12         *fileTorrentImpl
13         p metainfo.Piece
14         io.WriterAt
15         io.ReaderAt
16 }
17
18 var _ PieceImpl = (*filePieceImpl)(nil)
19
20 func (me *filePieceImpl) pieceKey() metainfo.PieceKey {
21         return metainfo.PieceKey{me.infoHash, me.p.Index()}
22 }
23
24 func (fs *filePieceImpl) Completion() Completion {
25         c, err := fs.completion.Get(fs.pieceKey())
26         if err != nil {
27                 log.Printf("error getting piece completion: %s", err)
28                 c.Ok = false
29                 return c
30         }
31
32         verified := true
33         if c.Complete {
34                 // If it's allegedly complete, check that its constituent files have the necessary length.
35                 for _, fi := range extentCompleteRequiredLengths(fs.p.Info, fs.p.Offset(), fs.p.Length()) {
36                         s, err := os.Stat(fs.files[fi.fileIndex].path)
37                         if err != nil || s.Size() < fi.length {
38                                 verified = false
39                                 break
40                         }
41                 }
42         }
43
44         if !verified {
45                 // The completion was wrong, fix it.
46                 c.Complete = false
47                 fs.completion.Set(fs.pieceKey(), false)
48         }
49
50         return c
51 }
52
53 func (fs *filePieceImpl) MarkComplete() error {
54         return fs.completion.Set(fs.pieceKey(), true)
55 }
56
57 func (fs *filePieceImpl) MarkNotComplete() error {
58         return fs.completion.Set(fs.pieceKey(), false)
59 }