2016-03-28 17:38:30 +08:00
|
|
|
package storage
|
2015-02-09 21:14:52 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2016-03-28 17:38:30 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2015-02-09 21:14:52 +08:00
|
|
|
)
|
|
|
|
|
2016-05-09 13:46:48 +08:00
|
|
|
// File-based storage for torrents, that isn't yet bound to a particular
|
|
|
|
// torrent.
|
2016-09-12 15:26:39 +08:00
|
|
|
type fileClientImpl struct {
|
2017-03-16 22:24:54 +08:00
|
|
|
baseDir string
|
|
|
|
pathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string
|
2017-05-22 10:15:48 +08:00
|
|
|
pc PieceCompletion
|
2015-02-09 21:14:52 +08:00
|
|
|
}
|
|
|
|
|
2017-03-16 22:24:54 +08:00
|
|
|
// The Default path maker just returns the current path
|
|
|
|
func defaultPathMaker(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string {
|
|
|
|
return baseDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func infoHashPathMaker(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string {
|
|
|
|
return filepath.Join(baseDir, infoHash.HexString())
|
|
|
|
}
|
|
|
|
|
|
|
|
// All Torrent data stored in this baseDir
|
2016-09-02 13:10:57 +08:00
|
|
|
func NewFile(baseDir string) ClientImpl {
|
2017-05-22 10:15:48 +08:00
|
|
|
return NewFileWithCompletion(baseDir, pieceCompletionForDir(baseDir))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileWithCompletion(baseDir string, completion PieceCompletion) ClientImpl {
|
|
|
|
return newFileWithCustomPathMakerAndCompletion(baseDir, nil, completion)
|
2017-03-16 22:24:54 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 10:56:28 +08:00
|
|
|
// File storage with data partitioned by infohash.
|
2017-03-16 22:24:54 +08:00
|
|
|
func NewFileByInfoHash(baseDir string) ClientImpl {
|
|
|
|
return NewFileWithCustomPathMaker(baseDir, infoHashPathMaker)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allows passing a function to determine the path for storing torrent data
|
|
|
|
func NewFileWithCustomPathMaker(baseDir string, pathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string) ClientImpl {
|
2017-05-22 10:15:48 +08:00
|
|
|
return newFileWithCustomPathMakerAndCompletion(baseDir, pathMaker, pieceCompletionForDir(baseDir))
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFileWithCustomPathMakerAndCompletion(baseDir string, pathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string, completion PieceCompletion) ClientImpl {
|
2017-03-16 22:24:54 +08:00
|
|
|
if pathMaker == nil {
|
|
|
|
pathMaker = defaultPathMaker
|
|
|
|
}
|
2016-09-12 15:26:39 +08:00
|
|
|
return &fileClientImpl{
|
2017-03-16 22:24:54 +08:00
|
|
|
baseDir: baseDir,
|
|
|
|
pathMaker: pathMaker,
|
2017-05-22 10:15:48 +08:00
|
|
|
pc: completion,
|
2016-03-28 17:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 16:07:26 +08:00
|
|
|
func (me *fileClientImpl) Close() error {
|
|
|
|
return me.pc.Close()
|
|
|
|
}
|
|
|
|
|
2016-09-12 15:26:39 +08:00
|
|
|
func (fs *fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) {
|
2017-03-16 22:24:54 +08:00
|
|
|
dir := fs.pathMaker(fs.baseDir, info, infoHash)
|
|
|
|
err := CreateNativeZeroLengthFiles(info, dir)
|
2016-09-12 15:01:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-12 15:26:39 +08:00
|
|
|
return &fileTorrentImpl{
|
2017-03-16 22:24:54 +08:00
|
|
|
dir,
|
2016-08-26 18:29:05 +08:00
|
|
|
info,
|
|
|
|
infoHash,
|
2016-10-25 16:07:26 +08:00
|
|
|
fs.pc,
|
2016-07-08 22:36:32 +08:00
|
|
|
}, nil
|
2016-03-28 19:40:29 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 15:26:39 +08:00
|
|
|
type fileTorrentImpl struct {
|
2017-03-16 22:24:54 +08:00
|
|
|
dir string
|
2016-07-10 21:03:59 +08:00
|
|
|
info *metainfo.Info
|
2016-08-26 18:29:05 +08:00
|
|
|
infoHash metainfo.Hash
|
2017-05-22 10:15:48 +08:00
|
|
|
completion PieceCompletion
|
2016-03-28 19:40:29 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 15:26:39 +08:00
|
|
|
func (fts *fileTorrentImpl) Piece(p metainfo.Piece) PieceImpl {
|
2016-05-09 13:46:48 +08:00
|
|
|
// Create a view onto the file-based torrent storage.
|
2016-09-12 15:26:39 +08:00
|
|
|
_io := fileTorrentImplIO{fts}
|
2016-05-09 13:46:48 +08:00
|
|
|
// Return the appropriate segments of this.
|
2017-10-12 13:09:32 +08:00
|
|
|
return &filePieceImpl{
|
2016-07-08 22:36:32 +08:00
|
|
|
fts,
|
2016-03-28 17:38:30 +08:00
|
|
|
p,
|
|
|
|
missinggo.NewSectionWriter(_io, p.Offset(), p.Length()),
|
|
|
|
io.NewSectionReader(_io, p.Offset(), p.Length()),
|
|
|
|
}
|
2015-10-01 22:09:04 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 15:26:39 +08:00
|
|
|
func (fs *fileTorrentImpl) Close() error {
|
2016-03-28 19:40:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-12 15:01:00 +08:00
|
|
|
// Creates natives files for any zero-length file entries in the info. This is
|
|
|
|
// a helper for file-based storages, which don't address or write to zero-
|
|
|
|
// length files because they have no corresponding pieces.
|
2017-03-16 22:24:54 +08:00
|
|
|
func CreateNativeZeroLengthFiles(info *metainfo.Info, dir string) (err error) {
|
2016-09-12 15:01:00 +08:00
|
|
|
for _, fi := range info.UpvertedFiles() {
|
|
|
|
if fi.Length != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-16 22:24:54 +08:00
|
|
|
name := filepath.Join(append([]string{dir, info.Name}, fi.Path...)...)
|
2018-06-09 21:11:28 +08:00
|
|
|
os.MkdirAll(filepath.Dir(name), 0777)
|
2016-09-12 15:01:00 +08:00
|
|
|
var f io.Closer
|
|
|
|
f, err = os.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-09 13:46:48 +08:00
|
|
|
// Exposes file-based storage of a torrent, as one big ReadWriterAt.
|
2016-09-12 15:26:39 +08:00
|
|
|
type fileTorrentImplIO struct {
|
|
|
|
fts *fileTorrentImpl
|
2016-03-28 17:38:30 +08:00
|
|
|
}
|
|
|
|
|
2016-03-26 17:45:31 +08:00
|
|
|
// Returns EOF on short or missing file.
|
2016-09-12 15:26:39 +08:00
|
|
|
func (fst *fileTorrentImplIO) readFileAt(fi metainfo.FileInfo, b []byte, off int64) (n int, err error) {
|
2016-07-10 21:03:59 +08:00
|
|
|
f, err := os.Open(fst.fts.fileInfoName(fi))
|
2016-03-26 17:45:31 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// File missing is treated the same as a short file.
|
|
|
|
err = io.EOF
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
// Limit the read to within the expected bounds of this file.
|
|
|
|
if int64(len(b)) > fi.Length-off {
|
|
|
|
b = b[:fi.Length-off]
|
|
|
|
}
|
|
|
|
for off < fi.Length && len(b) != 0 {
|
|
|
|
n1, err1 := f.ReadAt(b, off)
|
|
|
|
b = b[n1:]
|
2015-02-09 21:14:52 +08:00
|
|
|
n += n1
|
2016-03-26 17:45:31 +08:00
|
|
|
off += int64(n1)
|
|
|
|
if n1 == 0 {
|
|
|
|
err = err1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only returns EOF at the end of the torrent. Premature EOF is ErrUnexpectedEOF.
|
2016-09-12 15:26:39 +08:00
|
|
|
func (fst fileTorrentImplIO) ReadAt(b []byte, off int64) (n int, err error) {
|
2016-07-10 21:03:59 +08:00
|
|
|
for _, fi := range fst.fts.info.UpvertedFiles() {
|
2016-03-26 17:45:31 +08:00
|
|
|
for off < fi.Length {
|
2016-04-19 12:11:11 +08:00
|
|
|
n1, err1 := fst.readFileAt(fi, b, off)
|
2016-03-26 17:45:31 +08:00
|
|
|
n += n1
|
|
|
|
off += int64(n1)
|
|
|
|
b = b[n1:]
|
|
|
|
if len(b) == 0 {
|
|
|
|
// Got what we need.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n1 != 0 {
|
|
|
|
// Made progress.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = err1
|
|
|
|
if err == io.EOF {
|
|
|
|
// Lies.
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2016-02-17 14:58:20 +08:00
|
|
|
return
|
2015-02-09 21:14:52 +08:00
|
|
|
}
|
2016-03-26 17:45:31 +08:00
|
|
|
off -= fi.Length
|
2015-02-09 21:14:52 +08:00
|
|
|
}
|
2016-02-17 14:58:20 +08:00
|
|
|
err = io.EOF
|
2015-02-09 21:14:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-12 15:26:39 +08:00
|
|
|
func (fst fileTorrentImplIO) WriteAt(p []byte, off int64) (n int, err error) {
|
2016-07-10 21:03:59 +08:00
|
|
|
for _, fi := range fst.fts.info.UpvertedFiles() {
|
2015-02-09 21:14:52 +08:00
|
|
|
if off >= fi.Length {
|
|
|
|
off -= fi.Length
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n1 := len(p)
|
|
|
|
if int64(n1) > fi.Length-off {
|
|
|
|
n1 = int(fi.Length - off)
|
|
|
|
}
|
2016-07-10 21:03:59 +08:00
|
|
|
name := fst.fts.fileInfoName(fi)
|
2018-06-09 21:11:28 +08:00
|
|
|
os.MkdirAll(filepath.Dir(name), 0777)
|
2015-02-09 21:14:52 +08:00
|
|
|
var f *os.File
|
2018-06-09 21:11:28 +08:00
|
|
|
f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE, 0666)
|
2015-02-09 21:14:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n1, err = f.WriteAt(p[:n1], off)
|
2017-09-12 16:31:10 +08:00
|
|
|
// TODO: On some systems, write errors can be delayed until the Close.
|
2015-02-09 21:14:52 +08:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n += n1
|
|
|
|
off = 0
|
|
|
|
p = p[n1:]
|
|
|
|
if len(p) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-12 15:26:39 +08:00
|
|
|
func (fts *fileTorrentImpl) fileInfoName(fi metainfo.FileInfo) string {
|
2017-03-16 22:24:54 +08:00
|
|
|
return filepath.Join(append([]string{fts.dir, fts.info.Name}, fi.Path...)...)
|
2015-02-09 21:14:52 +08:00
|
|
|
}
|