]> Sergey Matveev's repositories - btrtrc.git/blob - storage/file-piece.go
Big rename of files and types in storage
[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         if c.Complete {
32                 // If it's allegedly complete, check that its constituent files have the necessary length.
33                 for _, fi := range extentCompleteRequiredLengths(fs.p.Info, fs.p.Offset(), fs.p.Length()) {
34                         s, err := os.Stat(fs.files[fi.fileIndex].path)
35                         if err != nil || s.Size() < fi.length {
36                                 c.Complete = false
37                                 break
38                         }
39                 }
40         }
41         if !c.Complete {
42                 // The completion was wrong, fix it.
43                 fs.completion.Set(fs.pieceKey(), false)
44         }
45         return c
46 }
47
48 func (fs *filePieceImpl) MarkComplete() error {
49         return fs.completion.Set(fs.pieceKey(), true)
50 }
51
52 func (fs *filePieceImpl) MarkNotComplete() error {
53         return fs.completion.Set(fs.pieceKey(), false)
54 }