2024-03-17 21:00:34 +00:00
|
|
|
package datastorage
|
2023-12-31 22:54:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"git.kmsign.ru/royalcat/tstor/src/config"
|
2024-01-28 20:22:49 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/host/store"
|
2023-12-31 22:54:55 +00:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
|
|
|
)
|
|
|
|
|
2024-03-17 21:00:34 +00:00
|
|
|
func Setup(cfg config.TorrentClient) (DataStorage, storage.PieceCompletion, error) {
|
2024-01-07 17:09:56 +00:00
|
|
|
pcp := filepath.Join(cfg.MetadataFolder, "piece-completion")
|
2023-12-31 22:54:55 +00:00
|
|
|
if err := os.MkdirAll(pcp, 0744); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("error creating piece completion folder: %w", err)
|
|
|
|
}
|
2024-01-28 20:22:49 +00:00
|
|
|
pc, err := store.NewBadgerPieceCompletion(pcp)
|
2023-12-31 22:54:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("error creating servers piece completion: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO implement cache/storage switching
|
|
|
|
// cacheDir := filepath.Join(tcfg.DataFolder, "cache")
|
|
|
|
// if err := os.MkdirAll(cacheDir, 0744); err != nil {
|
|
|
|
// return nil, nil, fmt.Errorf("error creating piece completion folder: %w", err)
|
|
|
|
// }
|
|
|
|
// fc, err := filecache.NewCache(cacheDir)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, nil, fmt.Errorf("error creating cache: %w", err)
|
|
|
|
// }
|
|
|
|
// log.Info().Msg(fmt.Sprintf("setting cache size to %d MB", 1024))
|
|
|
|
// fc.SetCapacity(1024 * 1024 * 1024)
|
|
|
|
|
|
|
|
// rp := storage.NewResourcePieces(fc.AsResourceProvider())
|
|
|
|
// st := &stc{rp}
|
|
|
|
|
2024-01-07 17:09:56 +00:00
|
|
|
filesDir := cfg.DataFolder
|
|
|
|
if err := os.MkdirAll(filesDir, 0744); err != nil {
|
2023-12-31 22:54:55 +00:00
|
|
|
return nil, nil, fmt.Errorf("error creating piece completion folder: %w", err)
|
|
|
|
}
|
2024-01-07 17:09:56 +00:00
|
|
|
st := NewFileStorage(filesDir, pc)
|
2023-12-31 22:54:55 +00:00
|
|
|
|
2024-01-07 17:09:56 +00:00
|
|
|
// piecesDir := filepath.Join(cfg.DataFolder, ".pieces")
|
|
|
|
// if err := os.MkdirAll(piecesDir, 0744); err != nil {
|
|
|
|
// return nil, nil, fmt.Errorf("error creating piece completion folder: %w", err)
|
|
|
|
// }
|
|
|
|
// st := storage.NewMMapWithCompletion(piecesDir, pc)
|
2023-12-31 22:54:55 +00:00
|
|
|
|
|
|
|
return st, pc, nil
|
|
|
|
}
|