Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open only existing torrent files #4007

Merged
merged 5 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/downloader/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func readPeerID(db kv.RoDB) (peerID []byte, err error) {

func (cli *Protocols) Start(ctx context.Context, silent bool) error {
if err := BuildTorrentsAndAdd(ctx, cli.snapshotDir, cli.TorrentClient); err != nil {
return err
return fmt.Errorf("BuildTorrentsAndAdd: %w", err)
}

var sem = semaphore.NewWeighted(int64(cli.cfg.DownloadSlots))
Expand Down
2 changes: 1 addition & 1 deletion cmd/downloader/downloader/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *GrpcServer) Download(ctx context.Context, request *proto_downloader.Dow
mi := &metainfo.MetaInfo{AnnounceList: Trackers}
for _, it := range request.Items {
if it.TorrentHash == nil {
_, err := BuildTorrentAndAdd(ctx, it.Path, s.snapshotDir, s.t.TorrentClient)
err := BuildTorrentAndAdd(ctx, it.Path, s.snapshotDir, s.t.TorrentClient)
if err != nil {
return nil, err
}
Expand Down
43 changes: 24 additions & 19 deletions cmd/downloader/downloader/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,43 +97,47 @@ func allSegmentFiles(dir string) ([]string, error) {
}

// BuildTorrentFileIfNeed - create .torrent files from .seg files (big IO) - if .seg files were added manually
func BuildTorrentFileIfNeed(ctx context.Context, originalFileName string, root *dir.Rw) (err error) {
func BuildTorrentFileIfNeed(ctx context.Context, originalFileName string, root *dir.Rw) (ok bool, err error) {
f, err := snapshotsync.ParseFileName(root.Path, originalFileName)
if err != nil {
return err
return false, err
}
if f.To-f.From != snapshotsync.DEFAULT_SEGMENT_SIZE {
return nil
return false, nil
}
torrentFilePath := filepath.Join(root.Path, originalFileName+".torrent")
if _, err := os.Stat(torrentFilePath); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
return false, err
}
info := &metainfo.Info{PieceLength: torrentcfg.DefaultPieceSize}
if err := info.BuildFromFilePath(filepath.Join(root.Path, originalFileName)); err != nil {
return err
return false, err
}
if err != nil {
return err
return false, err
}
if err := CreateTorrentFile(root, info, nil); err != nil {
return err
return false, err
}
}
return nil
return true, nil
}

func BuildTorrentAndAdd(ctx context.Context, originalFileName string, snapshotDir *dir.Rw, client *torrent.Client) (*torrent.Torrent, error) {
if err := BuildTorrentFileIfNeed(ctx, originalFileName, snapshotDir); err != nil {
return nil, err
func BuildTorrentAndAdd(ctx context.Context, originalFileName string, snapshotDir *dir.Rw, client *torrent.Client) error {
ok, err := BuildTorrentFileIfNeed(ctx, originalFileName, snapshotDir)
if err != nil {
return fmt.Errorf("BuildTorrentFileIfNeed: %w", err)
}
if !ok {
return nil
}
torrentFilePath := filepath.Join(snapshotDir.Path, originalFileName+".torrent")
t, err := AddTorrentFile(ctx, torrentFilePath, client)
_, err = AddTorrentFile(ctx, torrentFilePath, client)
if err != nil {
return nil, err
return fmt.Errorf("AddTorrentFile: %w", err)
}
return t, nil
return nil
}

// BuildTorrentFilesIfNeed - create .torrent files from .seg files (big IO) - if .seg files were added manually
Expand All @@ -151,7 +155,10 @@ func BuildTorrentFilesIfNeed(ctx context.Context, snapshotDir *dir.Rw) error {
wg.Add(1)
go func(f string, i int) {
defer wg.Done()
errs <- BuildTorrentFileIfNeed(ctx, f, snapshotDir)
_, err = BuildTorrentFileIfNeed(ctx, f, snapshotDir)
if err != nil {
errs <- err
}

select {
default:
Expand Down Expand Up @@ -180,7 +187,7 @@ func BuildTorrentsAndAdd(ctx context.Context, snapshotDir *dir.Rw, client *torre
defer logEvery.Stop()
files, err := allSegmentFiles(snapshotDir.Path)
if err != nil {
return err
return fmt.Errorf("allSegmentFiles: %w", err)
}
errs := make(chan error, len(files)*2)
wg := &sync.WaitGroup{}
Expand All @@ -203,9 +210,7 @@ func BuildTorrentsAndAdd(ctx context.Context, snapshotDir *dir.Rw, client *torre
errs <- ctx.Err()
default:
}

_, err := BuildTorrentAndAdd(ctx, f, snapshotDir, client)
errs <- err
errs <- BuildTorrentAndAdd(ctx, f, snapshotDir, client)
}(f, i)
}
go func() {
Expand Down
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func New(stack *node.Node, config *ethconfig.Config, txpoolCfg txpool2.Config, l
return nil, err
}
if err := backend.downloadProtocols.Start(ctx, true); err != nil {
return nil, err
return nil, fmt.Errorf("downloadProtocols start: %w", err)
}

bittorrentServer, err := downloader.NewGrpcServer(backend.downloadProtocols.DB, backend.downloadProtocols, config.SnapshotDir)
Expand Down