8 "github.com/anacrolix/missinggo"
10 "github.com/anacrolix/torrent/metainfo"
13 type fileStorage struct {
15 completed map[[20]byte]bool
18 func NewFile(baseDir string) *fileStorage {
24 func (me *fileStorage) Piece(p metainfo.Piece) Piece {
25 _io := &fileStorageTorrent{
29 return &fileStoragePiece{
32 missinggo.NewSectionWriter(_io, p.Offset(), p.Length()),
33 io.NewSectionReader(_io, p.Offset(), p.Length()),
37 type fileStoragePiece struct {
44 func (me *fileStoragePiece) GetIsComplete() bool {
45 return me.completed[me.p.Hash()]
48 func (me *fileStoragePiece) MarkComplete() error {
49 if me.completed == nil {
50 me.completed = make(map[[20]byte]bool)
52 me.completed[me.p.Hash()] = true
56 type fileStorageTorrent struct {
61 // Returns EOF on short or missing file.
62 func (me *fileStorageTorrent) readFileAt(fi metainfo.FileInfo, b []byte, off int64) (n int, err error) {
63 f, err := os.Open(me.fileInfoName(fi))
64 if os.IsNotExist(err) {
65 // File missing is treated the same as a short file.
73 // Limit the read to within the expected bounds of this file.
74 if int64(len(b)) > fi.Length-off {
77 for off < fi.Length && len(b) != 0 {
78 n1, err1 := f.ReadAt(b, off)
90 // Only returns EOF at the end of the torrent. Premature EOF is ErrUnexpectedEOF.
91 func (me *fileStorageTorrent) ReadAt(b []byte, off int64) (n int, err error) {
92 for _, fi := range me.info.UpvertedFiles() {
94 n1, err1 := me.readFileAt(fi, b, off)
109 err = io.ErrUnexpectedEOF
119 func (me *fileStorageTorrent) WriteAt(p []byte, off int64) (n int, err error) {
120 for _, fi := range me.info.UpvertedFiles() {
121 if off >= fi.Length {
126 if int64(n1) > fi.Length-off {
127 n1 = int(fi.Length - off)
129 name := me.fileInfoName(fi)
130 os.MkdirAll(filepath.Dir(name), 0770)
132 f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE, 0660)
136 n1, err = f.WriteAt(p[:n1], off)
151 func (me *fileStorageTorrent) fileInfoName(fi metainfo.FileInfo) string {
152 return filepath.Join(append([]string{me.baseDir, me.info.Name}, fi.Path...)...)