diff --git a/cmd/tstor/main.go b/cmd/tstor/main.go index 8ded9b5..fa89413 100644 --- a/cmd/tstor/main.go +++ b/cmd/tstor/main.go @@ -73,10 +73,7 @@ func run(configPath string) error { log.Err(err).Msg("set priority failed") } - if err := os.MkdirAll(filepath.Join(conf.TorrentClient.MetadataFolder, "meta"), 0744); err != nil { - return fmt.Errorf("error creating metadata folder: %w", err) - } - rep, err := repository.NewTorrentMetaRepository(filepath.Join(conf.TorrentClient.MetadataFolder, "meta")) + rep, err := repository.NewTorrentMetaRepository(conf.TorrentClient.MetadataFolder) if err != nil { return err } diff --git a/src/host/repository/repository.go b/src/host/repository/repository.go index aac2021..e17ed86 100644 --- a/src/host/repository/repository.go +++ b/src/host/repository/repository.go @@ -2,6 +2,7 @@ package repository import ( "errors" + "path/filepath" "sync" "github.com/anacrolix/torrent/metainfo" @@ -10,34 +11,31 @@ import ( "github.com/philippgille/gokv/encoding" ) -type TorrentMetaRepository interface { +type TorrentsRepository interface { ExcludeFile(hash metainfo.Hash, file ...string) error ExcludedFiles(hash metainfo.Hash) ([]string, error) } -func NewTorrentMetaRepository(dir string) (TorrentMetaRepository, error) { - store, err := badgerdb.NewStore(badgerdb.Options{ - Dir: dir, +func NewTorrentMetaRepository(dir string) (TorrentsRepository, error) { + excludedFilesStore, err := badgerdb.NewStore(badgerdb.Options{ + Dir: filepath.Join(dir, "excluded-files"), Codec: encoding.JSON, }) + if err != nil { return nil, err } r := &torrentRepositoryImpl{ - store: store, + excludedFiles: excludedFilesStore, } return r, nil } type torrentRepositoryImpl struct { - m sync.RWMutex - store gokv.Store -} - -type torrentMeta struct { - ExludedFiles []string + m sync.RWMutex + excludedFiles gokv.Store } var ErrNotFound = errors.New("not found") @@ -46,27 +44,25 @@ func (r *torrentRepositoryImpl) ExcludeFile(hash metainfo.Hash, file ...string) r.m.Lock() defer r.m.Unlock() - var meta torrentMeta - found, err := r.store.Get(hash.AsString(), &meta) + var excludedFiles []string + found, err := r.excludedFiles.Get(hash.AsString(), &excludedFiles) if err != nil { return err } if !found { - meta = torrentMeta{ - ExludedFiles: file, - } + excludedFiles = []string{} } - meta.ExludedFiles = unique(append(meta.ExludedFiles, file...)) + excludedFiles = unique(append(excludedFiles, file...)) - return r.store.Set(hash.AsString(), meta) + return r.excludedFiles.Set(hash.AsString(), excludedFiles) } func (r *torrentRepositoryImpl) ExcludedFiles(hash metainfo.Hash) ([]string, error) { r.m.Lock() defer r.m.Unlock() - var meta torrentMeta - found, err := r.store.Get(hash.AsString(), &meta) + var excludedFiles []string + found, err := r.excludedFiles.Get(hash.AsString(), &excludedFiles) if err != nil { return nil, err } @@ -74,7 +70,7 @@ func (r *torrentRepositoryImpl) ExcludedFiles(hash metainfo.Hash) ([]string, err return nil, nil } - return meta.ExludedFiles, nil + return excludedFiles, nil } func unique[C comparable](intSlice []C) []C { diff --git a/src/host/torrent/service.go b/src/host/torrent/service.go index a3bc382..2f1761f 100644 --- a/src/host/torrent/service.go +++ b/src/host/torrent/service.go @@ -15,7 +15,7 @@ import ( type Service struct { c *torrent.Client - rep repository.TorrentMetaRepository + rep repository.TorrentsRepository // stats *Stats DefaultPriority types.PiecePriority @@ -24,7 +24,7 @@ type Service struct { addTimeout, readTimeout int } -func NewService(c *torrent.Client, rep repository.TorrentMetaRepository, addTimeout, readTimeout int) *Service { +func NewService(c *torrent.Client, rep repository.TorrentsRepository, addTimeout, readTimeout int) *Service { l := slog.With("component", "torrent-service") return &Service{ log: l, diff --git a/src/host/vfs/torrent.go b/src/host/vfs/torrent.go index ddb248c..88f8b4f 100644 --- a/src/host/vfs/torrent.go +++ b/src/host/vfs/torrent.go @@ -21,7 +21,7 @@ var _ Filesystem = &TorrentFs{} type TorrentFs struct { mu sync.Mutex t *torrent.Torrent - rep repository.TorrentMetaRepository + rep repository.TorrentsRepository readTimeout int @@ -31,7 +31,7 @@ type TorrentFs struct { resolver *resolver } -func NewTorrentFs(t *torrent.Torrent, rep repository.TorrentMetaRepository, readTimeout int) *TorrentFs { +func NewTorrentFs(t *torrent.Torrent, rep repository.TorrentsRepository, readTimeout int) *TorrentFs { return &TorrentFs{ t: t, rep: rep,