2016-05-16 16:44:58 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2020-01-09 19:58:23 +08:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2016-05-16 16:44:58 +08:00
|
|
|
"path"
|
2020-01-09 19:58:23 +08:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2016-05-16 16:44:58 +08:00
|
|
|
|
2020-01-09 19:58:23 +08:00
|
|
|
"github.com/anacrolix/missinggo/v2/resource"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
2016-05-16 16:44:58 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type piecePerResource struct {
|
2016-05-16 20:32:00 +08:00
|
|
|
p resource.Provider
|
2016-05-16 16:44:58 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 11:12:44 +08:00
|
|
|
func NewResourcePieces(p resource.Provider) ClientImplCloser {
|
2016-05-16 16:44:58 +08:00
|
|
|
return &piecePerResource{
|
|
|
|
p: p,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 13:10:57 +08:00
|
|
|
func (s *piecePerResource) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) {
|
2016-05-16 16:44:58 +08:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *piecePerResource) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-02 13:10:57 +08:00
|
|
|
func (s *piecePerResource) Piece(p metainfo.Piece) PieceImpl {
|
2016-05-16 16:44:58 +08:00
|
|
|
return piecePerResourcePiece{
|
2020-01-09 19:58:23 +08:00
|
|
|
mp: p,
|
|
|
|
rp: s.p,
|
2016-05-16 16:44:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type piecePerResourcePiece struct {
|
2020-01-09 19:58:23 +08:00
|
|
|
mp metainfo.Piece
|
|
|
|
rp resource.Provider
|
2016-05-16 16:44:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 13:09:32 +08:00
|
|
|
func (s piecePerResourcePiece) Completion() Completion {
|
2020-01-09 19:58:23 +08:00
|
|
|
fi, err := s.completed().Stat()
|
2017-10-12 13:09:32 +08:00
|
|
|
return Completion{
|
2020-01-09 19:58:23 +08:00
|
|
|
Complete: err == nil && fi.Size() == s.mp.Length(),
|
2017-10-12 13:09:32 +08:00
|
|
|
Ok: true,
|
|
|
|
}
|
2016-05-16 16:44:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s piecePerResourcePiece) MarkComplete() error {
|
2020-01-09 19:58:23 +08:00
|
|
|
incompleteChunks := s.getChunks()
|
|
|
|
err := s.completed().Put(io.NewSectionReader(incompleteChunks, 0, s.mp.Length()))
|
|
|
|
if err == nil {
|
|
|
|
for _, c := range incompleteChunks {
|
|
|
|
c.instance.Delete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2016-05-16 16:44:58 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 13:10:57 +08:00
|
|
|
func (s piecePerResourcePiece) MarkNotComplete() error {
|
2020-01-09 19:58:23 +08:00
|
|
|
return s.completed().Delete()
|
2016-09-02 13:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s piecePerResourcePiece) ReadAt(b []byte, off int64) (int, error) {
|
2017-10-12 13:09:32 +08:00
|
|
|
if s.Completion().Complete {
|
2020-01-09 19:58:23 +08:00
|
|
|
return s.completed().ReadAt(b, off)
|
2016-05-16 16:44:58 +08:00
|
|
|
}
|
2020-01-09 19:58:23 +08:00
|
|
|
return s.getChunks().ReadAt(b, off)
|
2016-05-16 16:44:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s piecePerResourcePiece) WriteAt(b []byte, off int64) (n int, err error) {
|
2020-01-09 19:58:23 +08:00
|
|
|
i, err := s.rp.NewInstance(path.Join(s.incompleteDirPath(), strconv.FormatInt(off, 10)))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
r := bytes.NewReader(b)
|
|
|
|
err = i.Put(r)
|
|
|
|
n = len(b) - r.Len()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type chunk struct {
|
|
|
|
offset int64
|
|
|
|
instance resource.Instance
|
|
|
|
}
|
|
|
|
|
|
|
|
type chunks []chunk
|
|
|
|
|
|
|
|
func (me chunks) ReadAt(b []byte, off int64) (int, error) {
|
|
|
|
for {
|
|
|
|
if len(me) == 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
if me[0].offset <= off {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
me = me[1:]
|
|
|
|
}
|
|
|
|
n, err := me[0].instance.ReadAt(b, off-me[0].offset)
|
|
|
|
if n == len(b) {
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
if err == nil || err == io.EOF {
|
|
|
|
n_, err := me[1:].ReadAt(b[n:], off+int64(n))
|
|
|
|
return n + n_, err
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s piecePerResourcePiece) getChunks() (chunks chunks) {
|
|
|
|
names, err := s.incompleteDir().Readdirnames()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, n := range names {
|
|
|
|
offset, err := strconv.ParseInt(n, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i, err := s.rp.NewInstance(path.Join(s.incompleteDirPath(), n))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
chunks = append(chunks, chunk{offset, i})
|
|
|
|
}
|
|
|
|
sort.Slice(chunks, func(i, j int) bool {
|
|
|
|
return chunks[i].offset < chunks[j].offset
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s piecePerResourcePiece) completed() resource.Instance {
|
|
|
|
i, err := s.rp.NewInstance(path.Join("completed", s.mp.Hash().HexString()))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s piecePerResourcePiece) incompleteDirPath() string {
|
|
|
|
return path.Join("incompleted", s.mp.Hash().HexString())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s piecePerResourcePiece) incompleteDir() resource.DirInstance {
|
|
|
|
i, err := s.rp.NewInstance(s.incompleteDirPath())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return i.(resource.DirInstance)
|
2016-05-16 16:44:58 +08:00
|
|
|
}
|