mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
change disk cache
This commit is contained in:
@@ -3,6 +3,7 @@ package torr
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -126,6 +127,20 @@ func RemTorrent(hashHex string) {
|
|||||||
hash := metainfo.NewHashFromHex(hashHex)
|
hash := metainfo.NewHashFromHex(hashHex)
|
||||||
bts.RemoveTorrent(hash)
|
bts.RemoveTorrent(hash)
|
||||||
RemTorrentDB(hash)
|
RemTorrentDB(hash)
|
||||||
|
if sets.BTsets.UseDisk &&
|
||||||
|
hashHex != "" &&
|
||||||
|
hashHex != "/" &&
|
||||||
|
sets.BTsets.TorrentsSavePath != "" &&
|
||||||
|
sets.BTsets.TorrentsSavePath != "/" {
|
||||||
|
|
||||||
|
name := filepath.Join(sets.BTsets.TorrentsSavePath, hashHex)
|
||||||
|
err := os.RemoveAll(name)
|
||||||
|
if err != nil {
|
||||||
|
log.TLogln("Error remove cache:", err)
|
||||||
|
} else {
|
||||||
|
log.TLogln("Remove cache from disk:", hashHex)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListTorrent() []*Torrent {
|
func ListTorrent() []*Torrent {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package torrstor
|
package torrstor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -39,9 +39,6 @@ type Cache struct {
|
|||||||
isRemove bool
|
isRemove bool
|
||||||
muRemove sync.Mutex
|
muRemove sync.Mutex
|
||||||
torrent *torrent.Torrent
|
torrent *torrent.Torrent
|
||||||
|
|
||||||
info *metainfo.Info
|
|
||||||
file *os.File
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const FileRangeNotDelete = 5 * 1024 * 1024
|
const FileRangeNotDelete = 5 * 1024 * 1024
|
||||||
@@ -60,7 +57,6 @@ func NewCache(capacity int64, storage *Storage) *Cache {
|
|||||||
|
|
||||||
func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
||||||
log.TLogln("Create cache for:", info.Name, hash.HexString())
|
log.TLogln("Create cache for:", info.Name, hash.HexString())
|
||||||
c.info = info
|
|
||||||
if c.capacity == 0 {
|
if c.capacity == 0 {
|
||||||
c.capacity = info.PieceLength * 4
|
c.capacity = info.PieceLength * 4
|
||||||
}
|
}
|
||||||
@@ -71,11 +67,9 @@ func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
|||||||
|
|
||||||
if settings.BTsets.UseDisk {
|
if settings.BTsets.UseDisk {
|
||||||
name := filepath.Join(settings.BTsets.TorrentsSavePath, hash.HexString())
|
name := filepath.Join(settings.BTsets.TorrentsSavePath, hash.HexString())
|
||||||
ff, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0666)
|
err := os.MkdirAll(name, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.TLogln("Error open file:", err)
|
log.TLogln("Error create dir:", err)
|
||||||
} else {
|
|
||||||
c.file = ff
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +78,18 @@ func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if settings.BTsets.UseDisk {
|
if settings.BTsets.UseDisk {
|
||||||
c.loadInfo()
|
name := filepath.Join(settings.BTsets.TorrentsSavePath, hash.HexString())
|
||||||
|
fs, err := ioutil.ReadDir(name)
|
||||||
|
if err == nil {
|
||||||
|
for _, f := range fs {
|
||||||
|
id, err := strconv.Atoi(f.Name())
|
||||||
|
if err == nil {
|
||||||
|
c.pieces[id].Size = f.Size()
|
||||||
|
c.pieces[id].Complete = f.Size() == c.pieceLength
|
||||||
|
c.pieces[id].Accessed = f.ModTime().Unix()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,10 +113,6 @@ func (c *Cache) Close() error {
|
|||||||
c.readers = nil
|
c.readers = nil
|
||||||
c.muReaders.Unlock()
|
c.muReaders.Unlock()
|
||||||
|
|
||||||
if c.file != nil {
|
|
||||||
c.file.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
utils.FreeOSMemGC()
|
utils.FreeOSMemGC()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -268,7 +269,24 @@ func (c *Cache) isIdInFileBE(ranges []Range, id int) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) loadPieces() {
|
// run only in cache on disk
|
||||||
|
func (c *Cache) LoadPiecesOnDisk() {
|
||||||
|
if c.torrent == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.isRemove {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.muRemove.Lock()
|
||||||
|
if c.isRemove {
|
||||||
|
c.muRemove.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.isRemove = true
|
||||||
|
defer func() { c.isRemove = false }()
|
||||||
|
c.muRemove.Unlock()
|
||||||
|
|
||||||
ranges := make([]Range, 0)
|
ranges := make([]Range, 0)
|
||||||
c.muReaders.Lock()
|
c.muReaders.Lock()
|
||||||
for r, _ := range c.readers {
|
for r, _ := range c.readers {
|
||||||
@@ -291,29 +309,25 @@ func (c *Cache) loadPieces() {
|
|||||||
pc++
|
pc++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if len(c.readers) == 0 {
|
||||||
|
limit := 5
|
||||||
func (c *Cache) loadInfo() {
|
pc := 0
|
||||||
name := c.file.Name() + ".info"
|
end := c.pieceCount
|
||||||
buf, err := ioutil.ReadFile(name)
|
for pc <= end {
|
||||||
if err == nil {
|
if !c.pieces[pc].Complete {
|
||||||
var pieces map[int]*Piece
|
break
|
||||||
err := json.Unmarshal(buf, &pieces)
|
|
||||||
if err == nil {
|
|
||||||
for id, p := range pieces {
|
|
||||||
c.pieces[id].Size = p.Size
|
|
||||||
c.pieces[id].Complete = p.Complete
|
|
||||||
c.pieces[id].Accessed = p.Accessed
|
|
||||||
}
|
}
|
||||||
|
pc++
|
||||||
|
}
|
||||||
|
for pc <= end && limit > 0 {
|
||||||
|
if !c.pieces[pc].Complete {
|
||||||
|
if c.torrent.PieceState(pc).Priority == torrent.PiecePriorityNone {
|
||||||
|
c.torrent.Piece(pc).SetPriority(torrent.PiecePriorityNormal)
|
||||||
|
}
|
||||||
|
limit--
|
||||||
|
}
|
||||||
|
pc++
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Cache) saveInfo() {
|
|
||||||
buf, err := json.Marshal(c.pieces)
|
|
||||||
if err == nil {
|
|
||||||
name := c.file.Name() + ".info"
|
|
||||||
ioutil.WriteFile(name, buf, 0666)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,38 +1,41 @@
|
|||||||
package torrstor
|
package torrstor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"server/log"
|
||||||
|
"server/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DiskPiece struct {
|
type DiskPiece struct {
|
||||||
piece *Piece
|
piece *Piece
|
||||||
|
|
||||||
|
file *os.File
|
||||||
|
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDiskPiece(p *Piece) *DiskPiece {
|
func NewDiskPiece(p *Piece) *DiskPiece {
|
||||||
return &DiskPiece{piece: p}
|
name := filepath.Join(settings.BTsets.TorrentsSavePath, p.cache.hash.HexString(), strconv.Itoa(p.Id))
|
||||||
|
ff, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.TLogln("Error open file:", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &DiskPiece{piece: p, file: ff}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *DiskPiece) WriteAt(b []byte, off int64) (n int, err error) {
|
func (p *DiskPiece) WriteAt(b []byte, off int64) (n int, err error) {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
id := int64(p.piece.Id)
|
n, err = p.file.WriteAt(b, off)
|
||||||
pl := p.piece.cache.pieceLength
|
|
||||||
poff := id * pl
|
|
||||||
off += poff
|
|
||||||
|
|
||||||
n = 0
|
go p.piece.cache.LoadPiecesOnDisk()
|
||||||
off, err = p.piece.cache.file.Seek(off, io.SeekStart)
|
|
||||||
if err == nil {
|
|
||||||
n, err = p.piece.cache.file.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
go p.piece.cache.loadPieces()
|
|
||||||
p.piece.cache.saveInfo()
|
|
||||||
|
|
||||||
p.piece.Size += int64(n)
|
p.piece.Size += int64(n)
|
||||||
p.piece.Accessed = time.Now().Unix()
|
p.piece.Accessed = time.Now().Unix()
|
||||||
@@ -43,21 +46,12 @@ func (p *DiskPiece) ReadAt(b []byte, off int64) (n int, err error) {
|
|||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
id := int64(p.piece.Id)
|
n, err = p.file.ReadAt(b, off)
|
||||||
pl := p.piece.cache.pieceLength
|
|
||||||
poff := id * pl
|
|
||||||
off += poff
|
|
||||||
|
|
||||||
n = 0
|
|
||||||
off, err = p.piece.cache.file.Seek(off, io.SeekStart)
|
|
||||||
if err == nil {
|
|
||||||
n, err = p.piece.cache.file.Read(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
p.piece.Accessed = time.Now().Unix()
|
p.piece.Accessed = time.Now().Unix()
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *DiskPiece) Release() {
|
func (p *DiskPiece) Release() {
|
||||||
|
p.file.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,6 +109,7 @@ func (t *Torrent) WaitInfo() bool {
|
|||||||
case <-t.Torrent.GotInfo():
|
case <-t.Torrent.GotInfo():
|
||||||
t.cache = t.bt.storage.GetCache(t.Hash())
|
t.cache = t.bt.storage.GetCache(t.Hash())
|
||||||
t.cache.SetTorrent(t.Torrent)
|
t.cache.SetTorrent(t.Torrent)
|
||||||
|
go t.cache.LoadPiecesOnDisk()
|
||||||
return true
|
return true
|
||||||
case <-t.closed:
|
case <-t.closed:
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user