2016-03-28 17:38:30 +08:00
|
|
|
package storage
|
2015-02-09 21:14:52 +08:00
|
|
|
|
|
|
|
import (
|
2020-10-15 12:45:08 +08:00
|
|
|
"fmt"
|
2015-02-09 21:14:52 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2016-03-28 17:38:30 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
2020-05-31 22:14:51 +08:00
|
|
|
"github.com/anacrolix/torrent/common"
|
|
|
|
"github.com/anacrolix/torrent/segments"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
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
|
2020-02-21 11:12:44 +08:00
|
|
|
func NewFile(baseDir string) ClientImplCloser {
|
2017-05-22 10:15:48 +08:00
|
|
|
return NewFileWithCompletion(baseDir, pieceCompletionForDir(baseDir))
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:12:44 +08:00
|
|
|
func NewFileWithCompletion(baseDir string, completion PieceCompletion) *fileClientImpl {
|
2021-05-09 17:34:41 +08:00
|
|
|
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.
|
2021-05-07 23:35:33 +08:00
|
|
|
func NewFileByInfoHash(baseDir string) ClientImplCloser {
|
2017-03-16 22:24:54 +08:00
|
|
|
return NewFileWithCustomPathMaker(baseDir, infoHashPathMaker)
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:45:08 +08:00
|
|
|
// Allows passing a function to determine the path for storing torrent data. The function is
|
|
|
|
// responsible for sanitizing the info if it uses some part of it (for example sanitizing
|
|
|
|
// info.Name).
|
2021-05-07 23:35:33 +08:00
|
|
|
func NewFileWithCustomPathMaker(baseDir string, pathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string) ClientImplCloser {
|
2021-05-09 17:34:41 +08:00
|
|
|
return NewFileWithCustomPathMakerAndCompletion(baseDir, pathMaker, pieceCompletionForDir(baseDir))
|
2017-05-22 10:15:48 +08:00
|
|
|
}
|
|
|
|
|
2021-05-09 17:34:41 +08:00
|
|
|
// Allows passing custom PieceCompletion
|
|
|
|
func NewFileWithCustomPathMakerAndCompletion(baseDir string, pathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string, completion PieceCompletion) *fileClientImpl {
|
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()
|
|
|
|
}
|
|
|
|
|
2021-05-09 21:40:44 +08:00
|
|
|
func (fs *fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (_ TorrentImpl, err error) {
|
2017-03-16 22:24:54 +08:00
|
|
|
dir := fs.pathMaker(fs.baseDir, info, infoHash)
|
2020-05-31 22:14:51 +08:00
|
|
|
upvertedFiles := info.UpvertedFiles()
|
2020-10-15 12:45:08 +08:00
|
|
|
files := make([]file, 0, len(upvertedFiles))
|
|
|
|
for i, fileInfo := range upvertedFiles {
|
2021-05-09 21:40:44 +08:00
|
|
|
var s string
|
|
|
|
s, err = ToSafeFilePath(append([]string{info.Name}, fileInfo.Path...)...)
|
2020-10-15 12:45:08 +08:00
|
|
|
if err != nil {
|
2021-05-09 21:40:44 +08:00
|
|
|
err = fmt.Errorf("file %v has unsafe path %q: %w", i, fileInfo.Path, err)
|
|
|
|
return
|
2020-10-15 12:45:08 +08:00
|
|
|
}
|
|
|
|
f := file{
|
|
|
|
path: filepath.Join(dir, s),
|
|
|
|
length: fileInfo.Length,
|
|
|
|
}
|
|
|
|
if f.length == 0 {
|
|
|
|
err = CreateNativeZeroLengthFile(f.path)
|
|
|
|
if err != nil {
|
2021-05-09 21:40:44 +08:00
|
|
|
err = fmt.Errorf("creating zero length file: %w", err)
|
|
|
|
return
|
2020-10-15 12:45:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
2021-05-09 21:40:44 +08:00
|
|
|
t := &fileTorrentImpl{
|
2020-10-15 12:45:08 +08:00
|
|
|
files,
|
2020-05-31 22:14:51 +08:00
|
|
|
segments.NewIndex(common.LengthIterFromUpvertedFiles(upvertedFiles)),
|
2016-08-26 18:29:05 +08:00
|
|
|
infoHash,
|
2016-10-25 16:07:26 +08:00
|
|
|
fs.pc,
|
2021-05-09 21:40:44 +08:00
|
|
|
}
|
|
|
|
return TorrentImpl{
|
|
|
|
Piece: t.Piece,
|
|
|
|
Close: t.Close,
|
2016-07-08 22:36:32 +08:00
|
|
|
}, nil
|
2016-03-28 19:40:29 +08:00
|
|
|
}
|
|
|
|
|
2020-10-15 12:45:08 +08:00
|
|
|
type file struct {
|
|
|
|
// The safe, OS-local file path.
|
|
|
|
path string
|
|
|
|
length int64
|
|
|
|
}
|
|
|
|
|
2016-09-12 15:26:39 +08:00
|
|
|
type fileTorrentImpl struct {
|
2020-10-15 12:45:08 +08:00
|
|
|
files []file
|
2020-05-31 22:14:51 +08:00
|
|
|
segmentLocater segments.Index
|
|
|
|
infoHash metainfo.Hash
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:45:08 +08:00
|
|
|
// A helper to create zero-length files which won't appear for file-orientated storage since no
|
|
|
|
// writes will ever occur to them (no torrent data is associated with a zero-length file). The
|
|
|
|
// caller should make sure the file name provided is safe/sanitized.
|
|
|
|
func CreateNativeZeroLengthFile(name string) error {
|
|
|
|
os.MkdirAll(filepath.Dir(name), 0777)
|
|
|
|
var f io.Closer
|
|
|
|
f, err := os.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-09-12 15:01:00 +08:00
|
|
|
}
|
2020-10-15 12:45:08 +08:00
|
|
|
return f.Close()
|
2016-09-12 15:01:00 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2020-10-15 12:45:08 +08:00
|
|
|
func (fst *fileTorrentImplIO) readFileAt(file file, b []byte, off int64) (n int, err error) {
|
|
|
|
f, err := os.Open(file.path)
|
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.
|
2020-10-15 12:45:08 +08:00
|
|
|
if int64(len(b)) > file.length-off {
|
|
|
|
b = b[:file.length-off]
|
2016-03-26 17:45:31 +08:00
|
|
|
}
|
2020-10-15 12:45:08 +08:00
|
|
|
for off < file.length && len(b) != 0 {
|
2016-03-26 17:45:31 +08:00
|
|
|
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) {
|
2020-05-31 22:14:51 +08:00
|
|
|
fst.fts.segmentLocater.Locate(segments.Extent{off, int64(len(b))}, func(i int, e segments.Extent) bool {
|
2020-10-15 12:45:08 +08:00
|
|
|
n1, err1 := fst.readFileAt(fst.fts.files[i], b[:e.Length], e.Start)
|
2020-05-31 22:14:51 +08:00
|
|
|
n += n1
|
|
|
|
b = b[n1:]
|
|
|
|
err = err1
|
|
|
|
return err == nil // && int64(n1) == e.Length
|
|
|
|
})
|
|
|
|
if len(b) != 0 && err == nil {
|
|
|
|
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) {
|
2020-06-01 09:12:51 +08:00
|
|
|
//log.Printf("write at %v: %v bytes", off, len(p))
|
2020-05-31 22:14:51 +08:00
|
|
|
fst.fts.segmentLocater.Locate(segments.Extent{off, int64(len(p))}, func(i int, e segments.Extent) bool {
|
2020-10-15 12:45:08 +08:00
|
|
|
name := fst.fts.files[i].path
|
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 {
|
2020-05-31 22:14:51 +08:00
|
|
|
return false
|
2015-02-09 21:14:52 +08:00
|
|
|
}
|
2020-05-31 22:14:51 +08:00
|
|
|
var n1 int
|
|
|
|
n1, err = f.WriteAt(p[:e.Length], e.Start)
|
2020-06-01 09:12:51 +08:00
|
|
|
//log.Printf("%v %v wrote %v: %v", i, e, n1, err)
|
2020-05-31 22:14:51 +08:00
|
|
|
closeErr := f.Close()
|
2015-02-09 21:14:52 +08:00
|
|
|
n += n1
|
|
|
|
p = p[n1:]
|
2020-05-31 22:14:51 +08:00
|
|
|
if err == nil {
|
|
|
|
err = closeErr
|
2015-02-09 21:14:52 +08:00
|
|
|
}
|
2020-06-01 09:12:33 +08:00
|
|
|
if err == nil && int64(n1) != e.Length {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
2020-05-31 22:14:51 +08:00
|
|
|
return err == nil
|
|
|
|
})
|
2015-02-09 21:14:52 +08:00
|
|
|
return
|
|
|
|
}
|